我创建了一个类,它接受lwjgl的键盘输入并将其转换为字符串列表,这些字符串是当前正在按下的所有键。
public class KeyHandler {
ArrayList<String> keysPressed;
public KeyHandler() {
keysPressed = new ArrayList<String>();
}
public void checkKeys() {
while (Keyboard.next()) {
String keystring = Keyboard.getKeyName(Keyboard.getEventKey());
if (!keysPressed.contains(keystring)) {
keysPressed.add(keystring); // key has been pressed
} else {
keysPressed.remove(keystring); // key has been released
}
}
}
public void runKeys() {
if (keysPressed.size() > 0) {
for (String str : keysPressed) {
System.out.println("Key handler got key:" + str);
// run class for key
}
} else {
// no keys have been pressed
}
}
}
我正试图想办法让'runKeys'运行一个具有该名称的类,例如。
W.java
public class W {
public static void exc() {
player.moveZ(10);
}
}
这样做的原因是为了避免必须运行50+ if语句来检查输入
答案 0 :(得分:2)
Map<Character, Consumer<Character>>
。Consumer<Character>
接口的每个字符创建一个类。希望这有帮助。