嗨所以我有一个简单的任务就是使用箭头键在屏幕上移动四边形。我正在使用LWJGL来制作这个非常简单的游戏。"但是我遇到了一个问题。键盘输入有效但除了存在一个主要问题外,您无法按住箭头键。只有当您单击该键并立即释放它时它才会移动。坚持不做任何事。
这是我刚刚在游戏中使用的时钟课程:
public class Clock {
private static long lastFrame;
public static long getTime(){
return (Sys.getTime() * 1000) / Sys.getTimerResolution();
}
public static int getDelta(){
long currentTime = getTime();
int delta = (int) (currentTime - lastFrame);
lastFrame = getTime();
return delta;
}
public static void initLastFrame(){
lastFrame = getTime();
}
}
这是我在使用键盘时用于移动的方法:
public void move(){
while(Keyboard.next()){
if(Keyboard.getEventKeyState()){
if(Keyboard.getEventKey() == Keyboard.KEY_RIGHT){
this.x += this.speed * getDelta();
}
if(Keyboard.getEventKey() == Keyboard.KEY_UP){
this.y -= this.speed * getDelta();
}
if(Keyboard.getEventKey() == Keyboard.KEY_DOWN){
this.y += this.speed * getDelta();
}
else if(Keyboard.getEventKey() == Keyboard.KEY_LEFT){
this.x -= this.speed * getDelta();
}
}
}
}
答案 0 :(得分:0)
Keyboard.getEventKeyState()
如果键被按下,将返回true,如果键被释放则返回false。 你可以设置标志来决定你应该移动哪个方向。
if (Keyboard.getEventKey() == Keyboard.KEY_A) {
if (Keyboard.getEventKeyState()) {
movingLeft = true;
System.out.println("A Key Pressed");
}
else {
movingLeft = false;
System.out.println("A Key Released");
}
}
根据movingLeft.
对于这个问题,还有一个其他解决方案就是使用
Keyboard.isKeyDown(Keyboard.KEY_A)
漂亮自我解释,如果A失败则返回true,否则返回false。