J2ME KeyPressed

Discussion in 'Programming' started by yonip, Jun 24, 2011.

  1. #1
    Hi,
    I create a game. the game have main loop, that running all the game.
    I use the method KeyPressed of the GameCanvas, but when the main loop running, the KeyPressed method don't reacting.
    I can't check in the loop if any key pressed form anouther reasons.
    Anyone can help me?
    TIA.
     
    yonip, Jun 24, 2011 IP
  2. The Webby

    The Webby Peon

    Messages:
    1,852
    Likes Received:
    30
    Best Answers:
    1
    Trophy Points:
    0
    #2
    You have to poll for user input inside the game loop. The flow control can not leave the loop while the game state is running.
    I don't know if you have tried it, but you can create functions to update, draw and poll user input, then call them inside the game loop.
    Are you using any framework/engine or just coding through Native j2me?
     
    The Webby, Jun 24, 2011 IP
  3. yonip

    yonip Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I can't poll for user input inside the game loop. the game loop stop running when the game is over,
    and I need to check the keyPressed event also after the game over.
    I just coding through Native j2me.
    What can I do?
    Thanks.
     
    yonip, Jun 25, 2011 IP
  4. The Webby

    The Webby Peon

    Messages:
    1,852
    Likes Received:
    30
    Best Answers:
    1
    Trophy Points:
    0
    #4
    I do not understand, why can't you poll user input inside the game loop? Whats the problem?
     
    The Webby, Jun 25, 2011 IP
  5. yonip

    yonip Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    the problem is that I need check user input also after the game loop stoped.
     
    yonip, Jun 25, 2011 IP
  6. The Webby

    The Webby Peon

    Messages:
    1,852
    Likes Received:
    30
    Best Answers:
    1
    Trophy Points:
    0
    #6
    Is your game state based? Just create a method for polling user input, and call it in whatever state that is running currently..
    The app is still running after the game is over, right?
     
    The Webby, Jun 25, 2011 IP
    yonip likes this.
  7. yonip

    yonip Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    What is state based game?
    Can you explain?
    Thanks.
     
    yonip, Jun 26, 2011 IP
  8. The Webby

    The Webby Peon

    Messages:
    1,852
    Likes Received:
    30
    Best Answers:
    1
    Trophy Points:
    0
    #8
    A state is a kind of module of the game. It provides you flow management. You can create menu state, game state, game over state, etc. In simple words, It handles the current status of the flow control.
     
    The Webby, Jun 26, 2011 IP
  9. yonip

    yonip Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    how can I use this?
     
    yonip, Jun 26, 2011 IP
  10. The Webby

    The Webby Peon

    Messages:
    1,852
    Likes Received:
    30
    Best Answers:
    1
    Trophy Points:
    0
    #10
    Since you are not using any engine/framework, I guess you have to make a framework, that can define states, yourself..
    OR you can search for suitable j2me frameworks for games..

    I do not code in j2me, so I dont have much idea about it.. But I made games in UDK, XNA, Android and AS3 so I can only guide you through generalized game development and flow control.
     
    The Webby, Jun 26, 2011 IP
  11. yonip

    yonip Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Ok' thanks.
    Only one more question.
    I have 2 Canvases in my game: menu and game.
    The display show first the menu. When the user click "start", i change the current canvas to game, and call the main loop.
    Like this:
    d.setCurrent(game);
    game.loop();

    The result is: the game canvas doesn't shown and the menu canvas stuck.
    I guess the problem is that the display don't change the shown canvas until the main loop stopped.
    The main loop doesn't stopped because the game don't started, so the game canvas don't shown.
    Do you have any idea what can I do?
    TIA.
     
    yonip, Jun 26, 2011 IP
  12. The Webby

    The Webby Peon

    Messages:
    1,852
    Likes Received:
    30
    Best Answers:
    1
    Trophy Points:
    0
    #12
    I was stuck in same situation once, in my early days of game programming.. My game flow wasn't switching to the Game state from menu state, no matter what I tried, it wasn't working.. At last it turned out that I was doing everything except to update the screen. I wasn't calling Draw method after switching the states, thus Screen was stuck with Menu graphics even when the game was in game state..
     
    The Webby, Jun 26, 2011 IP
  13. yonip

    yonip Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    But in the game.loop() I call the Draw method. And it is until don't work.
     
    yonip, Jun 26, 2011 IP
  14. The Webby

    The Webby Peon

    Messages:
    1,852
    Likes Received:
    30
    Best Answers:
    1
    Trophy Points:
    0
    #14
    I cant say much about that, even if I look into your code I wont understand much because as I said I do not code in J2ME.. I can't be much of a help there mate.
    I suggest you look for an active J2ME community, they can provide you some real help.
     
    The Webby, Jun 26, 2011 IP
  15. yonip

    yonip Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    OK. I will look for one.
    Thanks. :)
     
    yonip, Jun 26, 2011 IP