gpt4 book ai didi

java - 如果玩家 2 向另一个方向移动,玩家 1 就会停止移动

转载 作者:行者123 更新时间:2023-12-02 02:42:10 28 4
gpt4 key购买 nike

如果我向上移动玩家 1 和玩家 2,假设我按下玩家 1 的向下键,我的玩家将停止向上移动。我找不到问题所在。有人可以帮助我并解释我做错了什么吗?

package game;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class KeyInput extends KeyAdapter{

private Handler handler;
private boolean [] keyPressed = new boolean [4];

public KeyInput(Handler handler){
this.handler = handler;
}
public void keyPressed(KeyEvent e){
int key = e.getKeyCode();

keyPressed[0]= false;
keyPressed[1]= false;
keyPressed[2]= false;
keyPressed[3]= false;

for(int i = 0; i <handler.object.size(); i++){
GameObject tempobject= handler.object.get(i);
if (tempobject.getId()== ID.Player1){
if (key == KeyEvent.VK_UP){tempobject.setSpeedy(-7); keyPressed[0] = true;}
if (key == KeyEvent.VK_DOWN){tempobject.setSpeedy(7); keyPressed[1] = true;}
}
if(tempobject.getId()== ID.player2)
if (key == KeyEvent.VK_W){tempobject.setSpeedy2(-7); keyPressed[2] = true;}
if (key == KeyEvent.VK_S){tempobject.setSpeedy2(7); keyPressed[3] = true;}
}
if(key == KeyEvent.VK_ESCAPE)System.exit(1);
}
public void keyReleased(KeyEvent e){
int key = e.getKeyCode();
for(int i = 0; i <handler.object.size(); i++){
GameObject tempobject= handler.object.get(i);
if (tempobject.getId()== ID.Player1){
if (key == KeyEvent.VK_UP) keyPressed[0] = false;
if (key == KeyEvent.VK_DOWN) keyPressed[1] = false;

if(!keyPressed[0] && !keyPressed[1])tempobject.setSpeedy(0);

}
if (tempobject.getId()== ID.player2){
if (key == KeyEvent.VK_W) keyPressed[2] = false;
if (key == KeyEvent.VK_S) keyPressed[3] = false;

if(!keyPressed[2] && !keyPressed[3])tempobject.setSpeedy2(0);
}
}
}
}

最佳答案

可能与此有关:

keyPressed[0]= false;
keyPressed[1]= false;
keyPressed[2]= false;
keyPressed[3]= false;

这将使得每当按下任何键时,其他键都会设置为 false,即使它们可能仍然被按住。

关于java - 如果玩家 2 向另一个方向移动,玩家 1 就会停止移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45287191/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com