我正在构建一个非常简单的游戏来练习我的基本Java知识,而我的KeyManager
课程没有响应。
package me.holdenew.game.keyinputs;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class KeyManager implements KeyListener {
private boolean[] keys;
public boolean up, down, left, right;
public KeyManager() {
keys = new boolean[256];
}
public void update() {
up = keys[KeyEvent.VK_W] || keys[KeyEvent.VK_UP];
down = keys[KeyEvent.VK_S] || keys[KeyEvent.VK_DOWN];
left = keys[KeyEvent.VK_A] || keys[KeyEvent.VK_LEFT];
right = keys[KeyEvent.VK_D] || keys[KeyEvent.VK_RIGHT];
}
@Override
public void keyPressed(KeyEvent e) {
keys[e.getKeyCode()] = true;
System.out.println(Integer.toString(e.getKeyCode()) + " pressed");
}
@Override
public void keyReleased(KeyEvent e) {
keys[e.getKeyCode()] = false;
System.out.println(Integer.toString(e.getKeyCode()) + " released");
}
@Override
public void keyTyped(KeyEvent e) {
}
}
我添加了print
语句,当我按下键时会调用它们,因此我知道该类已正确设置。但是,如果我一次按下多个键或按下一个按键并按下内容,那么所有键都没有响应,只调用keyReleased
print语句,而不是{{1一个。我可以帮忙解决这个问题吗?