我正在尝试为简单的桨式游戏实施作弊码,只有在按下特定的按键顺序时才能激活作弊:UP,UP,DOWN,DOWN,LEFT,LEFT,RIGHT,RIGHT。我正在努力,但我无法得到它!
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");
}
计划每50毫秒更新一次。 向上,向左,向下,向右是键入键,当按下这些键时,它会像计数器一样被添加。忽略整个g.fillRect和g.setColor以及System.out(试图看看它是如何起作用以及如何自己解决问题)。当用户没有输入正确的键顺序时,计数器应该重置,但由于更新是每50ms一次,它会搞砸计数器。
有没有办法检测按键的特定顺序,或者这是不可能的?
答案 0 :(得分:0)
每次单击按钮时,都应该创建一个接受KeyEvent的Object。该对象将按顺序跟踪已按下的当前键列表,并且还会在每次添加时检查堆栈以查看列表是否与您提供的模式匹配。如果用户输入的密钥与模式不匹配,请不要忘记清除堆栈!
答案 1 :(得分: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
答案 2 :(得分: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;
}
}