[在此处输入图片描述] [1]我刚刚开始按照教程系列来学习如何创建Java 3D游戏引擎,但是当Youtuber运行以下代码时,他的程序运行流畅,并且不在乎任何东西。但是,当我运行完全相同的代码时,它首先具有2 FPS,然后崩溃并显示错误消息“ Java(TM)二进制SE没有响应...
教程系列: post
代码:
import java.util.ArrayList;
import org.lwjgl.input.Keyboard;
public class Input {
public static final int NUM_KEYCODES = 256;
private static ArrayList<Integer> currentKeys = new ArrayList<Integer>();
private static ArrayList<Integer> downKeys = new ArrayList<Integer>();
private static ArrayList<Integer> upKeys = new ArrayList<Integer>();
//I call this method in my main method and this is also what causes the lag
// I think it has to do with the indexing of the keys, but on his computer, it runs smoothly
public static void update() {
// these 3 blocks of code clear out the Arraylists above and check if a is pressed / pressed down / or released
upKeys.clear();
for(int i = 0; i < NUM_KEYCODES; i++)
if(!getKey(i) && currentKeys.contains(i))
upKeys.add(i);
downKeys.clear();
for(int i = 0; i < NUM_KEYCODES; i++)
if(getKey(i) && !currentKeys.contains(i))
downKeys.add(i);
currentKeys.clear();
for(int i = 0; i < NUM_KEYCODES; i++)
if(getKey(i))
currentKeys.add(i);
}
public static boolean getKey(int keyCode) {
return Keyboard.isKeyDown(keyCode);
}
public static boolean getKeyDown(int keyCode) {
if(downKeys.contains(keyCode))
return true;
else
return false;
}
public static boolean getKeyUp(int keyCode) {
if(upKeys.contains(keyCode))
return true;
else
return false;
}
}```
[1]: https://i.stack.imgur.com/P7POD.jpg