0

嗨,我正在制作一个蛇游戏作为初学者 android 项目,我想要的功能之一是让音量控制键控制蛇。我编写了代码来处理控制,但是当我尝试玩游戏时,它会出现音量菜单而不是控制蛇。我想知道是否有某种方法可以覆盖音量键的原始操作。这是我处理控件的代码部分

      @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);
}
4

1 回答 1

4

处理完音量键后返回true,而不是通过调用该super方法。

if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
    // handle the keypress here
    return true;
}
于 2012-07-20T13:16:08.610 回答