禁用音量摇杆,以便它可用于控制游戏

时间:2012-07-20 13:13:48

标签: android button volume

嗨我正在制作蛇游戏作为初学者的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);
}

1 个答案:

答案 0 :(得分:4)

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

if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
    // handle the keypress here
    return true;
}