2014-04-10 62 views
0

我試圖實現一個簡單的槳遊戲的作弊碼,作弊時只應按下按鍵的特定順序時激活:向上,向下,向下,向左,向左,向右,向右。我正在嘗試,但我無法得到它!Java - 是否有可能讓它監聽特定的按鍵順序?

if(up >= 2){ 
     if(down >= 2){ 
      if(left >= 2){ 
       if(right >= 2){ 
        cheat = true; 
        g.setColor(0x00FF0000); 
        g.fillRect(x, y, canvas.getWidth(), 5); 
        g.fillRect(x, y - 5, 5, 5); 
        g.fillRect(canvas.getWidth() - 5, y - 5, 5, 5); 
        x = 0; 
        width = canvas.getWidth(); 
        height = canvas.getHeight(); 
       }else{ 
        g.setColor(0x00FF0000); 
        g.fillRect(x, y, width, height); 
        g.fillRect(x, y - height, height, height); 
        g.fillRect(x + width - height, y - height, height, height); 
        System.out.println(up + " " + down + " " + left + " " + right + "right"); 
       } 
      }else{ 
       g.setColor(0x00FF0000); 
       g.fillRect(x, y, width, height); 
       g.fillRect(x, y - height, height, height); 
       g.fillRect(x + width - height, y - height, height, height); 
       right = 0;System.out.println(up + " " + down + " " + left + " " + right + "left"); 
      } 
     }else{ 
      g.setColor(0x00FF0000); 
      g.fillRect(x, y, width, height); 
      g.fillRect(x, y - height, height, height); 
      g.fillRect(x + width - height, y - height, height, height); 
      left = 0; right = 0;System.out.println(up + " " + down + " " + left + " " + right + "down"); 
     } 
    }else{ 
     g.setColor(0x00FF0000); 
     g.fillRect(x, y, width, height); 
     g.fillRect(x, y - height, height, height); 
     g.fillRect(x + width - height, y - height, height, height); 
     down = 0; left = 0; right = 0;System.out.println(up + " " + down + " " + left + " " + right + "up"); 
    } 

程序每隔50ms更新一次。 向上,向左,向下,向右是按鍵,當按下這些按鍵時,它會像計數器一樣添加。忽略整個g.fillRect和g.setColor和System.out(試圖看看它是如何行事的以及如何自行解決問題)。當用戶沒有輸入正確的密鑰順序時,計數器應該重置,但由於每50ms更新一次,它會將計數器擰緊。

有沒有辦法來檢測按鍵的特定順序,或者它是不可能的?

+0

每次按下某個鍵時都會得到一個鍵碼。現在,你爲什麼不親自檢查訂單並激活它? – TheLostMind

回答

0

你可以做這樣的事情

int cheat = 0; //keeps track of how many were pressed in order 

if (cheat == 0 && upButtonPressed) 
    cheat++; 
elseif (cheat == 1 && upButtonPressed) 
    cheat++; 
elseif(cheat == 2 && downButtonPressed) 
    cheat++; 
//rest of the order 
else 
    cheat = 0; 

if (cheat == 8) //or however many buttons need to be pressed 
    //do whatever you want to happen when the cheat is activated 
+0

顯然所有這些都是在每次發生關鍵事件時都會調用的方法 – yitzih

+0

啊,非常感謝你!這工作很好。 – user3296002

0

您應該創建一個Object,每次單擊一個按鈕時都需要一個KeyEvent。對象將按順序跟蹤當前已按下的鍵列表,並且每次添加時都會檢查堆棧以查看列表是否與您提供的模式匹配。如果用戶輸入與模式不匹配的密鑰,請不要忘記清除堆棧!

0

我會做兩個數組與鍵碼。一個用於遊戲動作代碼,另一個用於相應的鍵碼。 (因爲您希望提供一種在沒有D-pad或遊戲杆的手機上輸入組合鍵的替代方式)。

int[] keys1 = {getKeyCode(UP), getKeyCode(UP), getKeyCode(DOWN), getKeyCode(DOWN), getKeyCode(LEFT), getKeyCode(LEFT), getKeyCode(RIGHT), getKeyCode(RIGHT)}; 
int[] keys2 = {KEY_NUM2, KEY_NUM2, KEY_NUM8, KEY_NUM8, KEY_NUM4, KEY_NUM4, KEY_NUM6, KEY_NUM6}; 
int keyIterator = 0; 

public void keyPressed(int kc) { 
if (kc == keys1[keyIterator] || kc == keys2[keyIterator]) { // Correct key 
    keyIterator++; 
    if (keyIterator > keys1.length) { 
    // keyIterator has reached the length of the array, meaning the secret key combination has been entered successfully. 
    } 
} else { // Wrong key. Start over 
    keyIterator = 0; 
} 
} 
相關問題