我正在测试一个试图将物理键盘键绑定到它的游戏。我实现了onKeyUp和onKeyDown来知道用户何时按下了移动键。问题是onKeyDown事件每30-40ms被重复调用一次。
理论上,如果我在不执行onKeyUp的情况下按下按键,则必须只调用一次,但是在我执行keyUp之前它会被调用很多次。
有什么问题?这是代码:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
int factor = GameState.getInstance().getSpaceship().getSpeedFactorCorrespondence();
int movementRange = sh/factor;
switch (keyCode) {
case KeyEvent.KEYCODE_W:
Log.d("XX_KEYBOARD","onKeyDown KEYCODE_W "+keyCode);
GameState.getInstance().setJoyY(movementRange);
return true;
case KeyEvent.KEYCODE_A:
Log.d("XX_KEYBOARD","onKeyDown KEYCODE_A "+keyCode);
GameState.getInstance().setJoyX(-movementRange);
return true;
case KeyEvent.KEYCODE_S:
Log.d("XX_KEYBOARD","onKeyDown KEYCODE_S "+keyCode);
GameState.getInstance().setJoyY(-movementRange);
return true;
case KeyEvent.KEYCODE_D:
Log.d("XX_KEYBOARD","onKeyDown KEYCODE_D "+keyCode);
GameState.getInstance().setJoyX(movementRange);
return true;
case KeyEvent.KEYCODE_J:
Log.d("XX_KEYBOARD","onKeyDown KEYCODE_J "+keyCode);
touchX=sw/2;
touchY=sh/2;
LaserManager.getInstance().startLaserThread();
return true;
default:
Log.d("XX_KEYBOARD","onKeyDown default "+keyCode);
return super.onKeyDown(keyCode, event);
}
}