Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use <a href="http://www.lwjgl.org/javadoc/org/lwjgl/input/Keyboard.html#getEventKeyState%28%29" rel="nofollow"><code>Keyboard.getEventKeyState</code></a> to determine the current event, followed by <a href="http://www.lwjgl.org/javadoc/org/lwjgl/input/Keyboard.html#getEventKey%28%29" rel="nofollow"><code>Keyboard.getEventKey</code></a> to determine which key this is. Then, you need to make sure you disable repeat events via <a href="http://www.lwjgl.org/javadoc/org/lwjgl/input/Keyboard.html#enableRepeatEvents%28boolean%29" rel="nofollow"><code>Keyboard.enableRepeatEvents</code></a>. Maintain state for the current movement, changing based on these events, and every tick move accordingly. Something like the following, as quick sketch:</p> <pre><code>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 &amp;&amp; movement == dir) { movement = Direction.NONE; } ... /* later on, use movement to determine which direction to move */ </code></pre> <p>In the above example, <code>Direction.of</code> returns the appropriate direction for the pressed key,</p> <pre><code>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; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload