I am working on a game that relies on the player's keyboard input. The current implementation registers whether a key is currently pressed or not.
However, I would like to implement a system in which the player has to do a full key press in order to increment the game state. For example, they'd have to press and release 'A' in order to move to the next position. Then press and release the button to move on again, and so forth. Holding the button down should not result in moving forward.
Here is a very simplified version of what I've got right now:
int velocityX = 0;
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_A) {ActiveA = true;}
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_A) {ActiveA = false;}
}
public void tick() {
x += velocityX;
if (ActiveA){velocityX = 5; }
else if (!ActiveA){velocityX = 0; }
}
The game class calls tick() at about 60 times a second, so X constantly increments while the key is pressed but not while the key is released.