2012-07-20 31 views
0

嗨我做一個蛇遊戲作爲一個初學者的android項目,我想要的一個功能是使音量控制鍵控制蛇。我編寫了代碼來處理控制,但是當我嘗試玩遊戲時,它會出現音量菜單而不是控制蛇。我想知道是否有某種方法可以覆蓋音量鍵的原始操作。 赫雷什其中I處理控制禁用音量搖桿,所以它可以用來控制遊戲

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

你可以發佈包含此代碼的方法嗎?也許你是在宣佈它錯了,或者在錯誤的地方,但是隻有這一部分我們不能說出來。 – FoamyGuy 2012-07-20 13:38:30

+0

我更新了代碼 – user1526178 2012-07-20 13:45:53

回答

4

返回true處理體積按鍵,而不是落下通過調用super方法之後的代碼的一部分。

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

請您詳細說明一下嗎? – user1526178 2012-07-20 13:30:48

+0

不是真的......「處理音量按鍵後返回true」的操作非常簡單,因爲我無需爲您編寫代碼就可以做到這一點。 – 2012-07-20 13:33:08

+0

嘆息......好的,有一個你需要做的例子。除此之外,我真的不能再說了。 – 2012-07-20 13:34:59