Hey, I've just started to learn Android development and ran into a problem.
Controls in the game I'm making work on virtual device, but not on phone:
- 开发者_如何转开发I have an Xperia X10 Mini Pro
- I'm making a basic Pong game to learn droid software development
- The game works just fine on my Android virtual device, you can move the paddles up and down smoothly
- On my phone I've figured that the onKeyDown event doesn't run until I release the button or after I've held down the button for a second or two, but then it only registers it as a brief press of the button, not like if I was holding it down
- I believe that it's a feature of my phone to quickly access special characters, because some keys register different key codes when pressed quickly and when held down
- The problems this results in is that I can not move the paddles, but I can do single press things, like pause the game
I can also add:
- Game is heavily built around sample app LunarLander from the SDK
- Lunar lander works fine in virtual device but has same issues as my pong when put in my phone
How could I force my phone to register onKeyDown events like it should? (Or at least like the virtual device does?)
Code from my view class that extends surfaceView:
@Override
public boolean onKeyDown(int keyCode, KeyEvent msg) {
return thread.doKeyDown(keyCode, msg);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent msg) {
return thread.doKeyUp(keyCode, msg);
}
Code from within the thread:
boolean doKeyDown(int keyCode, KeyEvent msg) {
synchronized (mSurfaceHolder) {
mP1Score = keyCode;//For debugging
boolean okStart = false;
if (keyCode == KeyEvent.KEYCODE_SPACE) okStart = true;
if (mMode == STATE_PAUSE && okStart) {
setState(STATE_RUNNING);
return true;
} else if (mMode == STATE_READY && okStart) {
setState(STATE_RUNNING);
return true;
} else if (mMode == STATE_GOAL && okStart) {
return true;
}else if (mMode == STATE_END_GAME && okStart) {
doStart();
return true;
} else if (mMode == STATE_RUNNING) {
if (keyCode == KeyEvent.KEYCODE_Q) {
mP1dY = -PLAYER_SPEED;//Player 1 up
return true;
} else if (keyCode == KeyEvent.KEYCODE_A) {
mP1dY = PLAYER_SPEED;//Player 1 down
return true;
} else if (keyCode == KeyEvent.KEYCODE_O) {
mP2dY = -PLAYER_SPEED;//Player 2 up
return true;
} else if (keyCode == KeyEvent.KEYCODE_L) {
mP2dY = PLAYER_SPEED;//Player 2 down;
return true;
} else if (keyCode == KeyEvent.KEYCODE_SPACE) {
pause();//Space is pressed in game
return true;
}
}
return false;
}
}
boolean doKeyUp(int keyCode, KeyEvent msg) {
boolean handled = false;
mP2Score = keyCode;//For debugging
synchronized (mSurfaceHolder) {
if (mMode == STATE_RUNNING) {
if (keyCode == KeyEvent.KEYCODE_Q
|| keyCode == KeyEvent.KEYCODE_A) {
mP1dY = 0;
handled = true;
} else if (keyCode == KeyEvent.KEYCODE_O
|| keyCode == KeyEvent.KEYCODE_L) {
mP2dY = 0;
handled = true;
}
}
}
return handled;
}
Complete source available here
Here's a sort-of-solution:
The problem is SE's default input method on the X10 Mini Pro. By installing a different input method on the phone (HTC_IME in my case) I can get controls to work like they do on the virtual device.
Now I only need to figure out how to make it work with the default input method and I'll have a complete solution.
精彩评论