我正在使用Java制作2D游戏,我有一个关键的监听器来处理玩家的动作和其他东西。它大部分时间都完全正常。但是,有时会出现故障。例如,我将持有" A"使角色向左移动的关键,当我释放它时,角色会一直移动,就像我仍然按住键一样。我认为这是因为关键监听器由于某种原因没有检测到keyReleased事件。
我一直试图找出原因,但仍然不知道为什么会这样做。
如果你想看一下,我的代码就是其中的一部分。
package input;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import game.Game;
public class KeyboardInput extends KeyAdapter {
public KeyboardInput() {
}
// Detects pressed keys
public void keyPressed(KeyEvent e) {
if (Game.init) {
Keys.Keys[e.getKeyCode()] = true;
}
}
// Detects released keys
public void keyReleased(KeyEvent e) {
if (Game.init) {
Keys.Keys[e.getKeyCode()] = false;
}
}
}
// Display Constructor
public Display(String title, String gameVersion, int width, int height) {
this.title = title;
this.width = width;
this.height = height;
this.gameVersion = gameVersion;
createDisplay();
}
// Creates display
private void createDisplay() {
frame = new JFrame(title + " v. " + gameVersion);
frame.setSize(width, height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setFocusable(true);
canvas = new Canvas();
canvas.setPreferredSize(new Dimension(width, height));
canvas.setMinimumSize(new Dimension(width, height));
canvas.setMaximumSize(new Dimension(width, height));
canvas.setBackground(Color.WHITE);
frame.add(canvas);
frame.addKeyListener(new KeyboardInput()); // ADDING KEY LISTENER TO THE JFRAME
canvas.addMouseListener(new MouseInput());
frame.pack();
if (frame.getKeyListeners().length > 0) {
System.out.println("Key listener initialized");
} else {
System.out.println("ERROR : Key listener couldn't initialize");
}
if (canvas.getMouseListeners().length > 0) {
System.out.println("Mouse listener initialized");
} else {
System.out.println("ERROR : Mouse listener couldn't initialize");
}
}
// Updates display (called every frame)
public void update() {
frame.requestFocus();
tick++;
lasttime = currtime;
currtime = System.nanoTime();
delta = (currtime - lasttime) / 1000000000;
fps = (float) (1 / delta);
if ((tick / fps) >= SAVE_TIME) {
tick = 0;
saveNextTick = true;
System.out.println("FPS = " + Math.round(getFPS()));
try {
System.out.println("MOUSE POSITION = (" + frame.getMousePosition().getX() + ", "
+ frame.getMousePosition().getY() + ")");
} catch (Exception e) {
System.out.println("Mouse out of screen. Could not get mouse position (NullPointerException)");
}
}
if (saveNextTick) {
Save.saveGame("Save");
saveNextTick = false;
}
}
private void checkMove() {
int speed = moveSpeed;
int x1 = getxPos();
int x2 = x1 + getTexture().getImage().getWidth();
int y1 = getyPos();
int y2 = y1 + getTexture().getImage().getHeight();
if ((Keys.Keys[KeyEvent.VK_W] || Keys.Keys[KeyEvent.VK_S]) && (Keys.Keys[KeyEvent.VK_A] || Keys.Keys[KeyEvent.VK_D])) {
speed = diagonalMoveSpeed;
}
int d = Math.max((int) (speed / Display.getFPS()), 1);
if (speed / Display.getFPS() < 1 && Util.randInt(0, 100) > speed / Display.getFPS() * 100) {
d = 0;
}
if (Keys.Keys[KeyEvent.VK_W] && !States.currentState.checkCollision(x1, x2, y1 - d, y2 - d)) {
move(0, -d);
}
if (Keys.Keys[KeyEvent.VK_A] && !States.currentState.checkCollision(x1 - d, x2 - d, y1, y2)) {
move(-d, 0);
}
if (Keys.Keys[KeyEvent.VK_S] && !States.currentState.checkCollision(x1, x2, y1 + d, y2 + d)) {
move(0, d);
}
if (Keys.Keys[KeyEvent.VK_D] && !States.currentState.checkCollision(x1 + d, x2 + d, y1, y2)) {
move(d, 0);
}
}
提前感谢您的帮助。如果您需要查看代码的任何其他部分以找出问题,请询问。
P.S。 :我知道这可能不是编写关键监听器/获取键盘输入的最简单方法,但它是我所知道的唯一方式。我目前正在寻找更好的方法,一旦我做了改变,我就会改变它。
答案 0 :(得分:0)
你应该做一些像键盘去抖的东西。因为我们长时间按下键盘按钮。因为当我们尝试通过长按键盘键在编辑器中编写文本时,在我们释放密钥后会产生更多的字符。
阅读相应的质量保证书:
implementing debounce in Java