こんにちは私は初心者のAndroidプロジェクトとしてヘビゲームを作っています。私が欲しい機能の1つは、ボリュームコントロールキーでヘビを制御することです。コントロールを処理するコードを作成しましたが、ゲームをプレイしようとすると、ヘビをコントロールする代わりにボリュームメニューが表示されます。ボリュームキーの元のアクションをオーバーライドする方法があるかどうか疑問に思いました。これが私がコントロールを扱うコードの部分です
@Override
public boolean onKeyDown(int keyCode, KeyEvent msg) {
if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
if (mMode == READY) {
/*
* At the beginning of the game, or the end of a previous one,
* we should start a new game.
*/
initNewGame();
setMode(RUNNING);
update();
return (true);
}
if (mMode == PAUSE) {
/*
* If the game is merely paused, we should just continue where
* we left off.
*/
setMode(RUNNING);
update();
return (true);
}
if (mMode == PAUSED) {
/*
* If the game is merely paused by the user then we shall tell them their score, we should just continue where
* we left off.
*/
setMode(RUNNING);
update();
return (true);
}
if (mDirection != SOUTH) {
mNextDirection = NORTH;
}
return (true);
}
if (keyCode == KeyEvent.KEYCODE_MENU) {
if (mMode == RUNNING) {
/*
* At the beginning of the game, or the end of a previous one,
* we should start a new game.
*/
setMode(PAUSED);
update();
return (true);
}
if (mMode == PAUSED) {
/*
* If the game is merely paused by the user then we shall tell them their score, we should just continue where
* we left off.
*/
setMode(RUNNING);
update();
return (true);
}
}
if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
if (mMode == READY) {
/*
* At the beginning of the game, or the end of a previous one,
* we should start a new game.
*/
initNewGameChaos();
setMode(RUNNING);
update();
return (true);
}
if (mMode == PAUSED) {
/*
* If the game is merely paused by the user then we shall tell them their score, we should just continue where
* we left off.
*/
setMode(RUNNING);
update();
return (true);
}
if (mDirection != NORTH) {
mNextDirection = SOUTH;
}
return (true);
}
if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
if (mMode == READY) {
/*
* At the beginning of the game, or the end of a previous one,
* we should start a new game.
*/
initNewGameHard();
setMode(RUNNING);
update();
return (true);
}
if (mMode == PAUSED) {
setMode(RUNNING);
update();
return (true);
}
if (mDirection != EAST) {
mNextDirection = WEST;
}
return (true);
}
if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
if (mMode == READY) {
initNewGameEasy();
setMode(RUNNING);
update();
return (true);
}
if (mDirection != WEST) {
mNextDirection = EAST;
}
if (mMode == PAUSED) {
setMode(RUNNING);
update();
return (true);
}
return (true);
}
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
if (mMode == LOSE) {
setMode(READY);
update();
return (true);
}
if (mMode == RUNNING) {
setMode(PAUSED);
update();
return (true);
}
if (mMode == PAUSED) {
setMode(RUNNING);
update();
return (true);
}
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
if (mDirection == WEST) {
mNextDirection = SOUTH;
}
if (mDirection == SOUTH) {
mNextDirection = EAST;
}
if (mDirection == EAST) {
mNextDirection = NORTH;
}
if (mDirection != NORTH) {
mNextDirection = WEST;
}
if (mMode == PAUSED) {
setMode(RUNNING);
update();
return (true);
}
return true;
}
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
if (mDirection == WEST) {
mNextDirection = NORTH;
}
if (mDirection == NORTH) {
mNextDirection = EAST;
}
if (mDirection == EAST) {
mNextDirection = SOUTH;
}
if (mDirection != SOUTH) {
mNextDirection = WEST;
}
if (mMode == PAUSED) {
setMode(RUNNING);
update();
return (true);
}
return true;
}
}
return super.onKeyDown(keyCode, msg);
}