onKeyDown problems


Last Updated:

  1. aNewGuy

    aNewGuy New Member This Topic's Starter

    Joined:
    Jul 22, 2010
    Messages:
    3
    Likes Received:
    0
    I'm working on a simple game and I've just added a main menu. The problem is that when i click the "Play" button on my menu, the game launches but it doesn't receive key presses properly. My menu class still receives all key presses unless i press one of the arrow keys. After I press one of the arrow keys my game class starts receiving key presses as I expect it to.

    right now the code looks something like this

    Code (Text):
    1. public class gameTest extends Activity {
    2.     Board mGameBoard;
    3.     private Handler mHandler;
    4.  
    5.     @Override
    6.     public void onCreate(Bundle savedInstanceState) {
    7.         super.onCreate(savedInstanceState);
    8.        
    9.         //setup buttons
    10.         setContentView(R.layout.main);
    11.         final TextView btnPlay = (TextView) findViewById(R.id.mainMenuPlay);
    12.         btnPlay.setOnClickListener(new View.OnClickListener() {
    13.             public void onClick(View v) {
    14.                 Log.d("main", "button clicked");
    15.                 setContentView(mGameBoard);
    16.                 mHandler.post(update);
    17.             }
    18.         });
    19.        
    20.         mGameBoard = new Board(this);
    21.         mGameBoard.create();
    22.        
    23.         mHandler = new Handler();
    24.     }
    25.    
    26.     private Runnable update = new Runnable() {
    27.         public void run() {
    28.             mWorld.update();
    29.             mHandler.postDelayed(update, (long) (mGameBoard.timeStep*1000));
    30.         }
    31.     };
    32. }
    If i remove the button listener and just skip straight to the game view at works as expected ie:

    Code (Text):
    1. public void onCreate(Bundle savedInstanceState) {
    2.         super.onCreate(savedInstanceState);
    3.        
    4.         //setup buttons
    5.         setContentView(R.layout.main);
    6.         /*
    7.         final TextView btnPlay = (TextView) findViewById(R.id.mainMenuPlay);
    8.         btnPlay.setOnClickListener(new View.OnClickListener() {
    9.             public void onClick(View v) {
    10.                 Log.d("main", "button clicked");
    11.                 setContentView(mGameBoard);
    12.                 mHandler.post(update);
    13.             }
    14.         });
    15.         */
    16.         mGameBoard = new Board(this);
    17.         mGameBoard.create();
    18.         setContentView(mGameBoard);
    19.         mHandler.post(update);
    20.        
    21.         mHandler = new Handler();
    22.     }
    That makes the game catch all key presses fine (though, obviously no menu at the start). The game board class just uses a onkeydown() to catch key press events.

    This is my first time working on android so i'm not sure about quite a few things. Anyone know what might be wrong? If you need more info just ask.
     

    Advertisement

Share This Page

Loading...