現在のイベントを判別するために使用Keyboard.getEventKeyState
し、続いてKeyboard.getEventKey
これがどのキーであるかを判別します。次に、を介して繰り返しイベントを無効にする必要がありますKeyboard.enableRepeatEvents
。現在の動きの状態を維持し、これらのイベントに基づいて変化し、すべてのティックがそれに応じて動きます。簡単なスケッチとして、次のようなもの:
Keyboard.enableRepeatEvents(false);
...
/* in your game update routine */
final int key = Keyboard.getEventKey();
final boolean pressed = Keyboard.getEventKeyState();
final Direction dir = Direction.of(key);
if (pressed) {
movement = dir;
} else if (movement != Direction.NONE && movement == dir) {
movement = Direction.NONE;
}
...
/* later on, use movement to determine which direction to move */
上記の例でDirection.of
は、押されたキーの適切な方向を返します。
enum Direction {
NONE, LEFT, RIGHT, DOWN, UP;
static Direction of(final int key) {
switch (key) {
case Keyboard.KEY_A:
return Direction.LEFT;
case Keyboard.KEY_D:
return Direction.RIGHT;
case Keyboard.KEY_W:
return Direction.UP;
case Keyboard.KEY_S:
return Direction.DOWN;
default:
return Direction.NONE;
}
}
}