Reputation: 1623
I have the following code snippet from my SWING game:
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_RIGHT)
{
player.setX(player.getX() + speed);
canvas.repaint();
}
The code is working correctly, when I press the right arrow(VK_RIGHT
),
the player moves RIGHT, if I press the left arrow it moves left.
The problem is when I hold the LEFT or RIGHT arrow, the player moves once and then it stops for some time and then it starts continuously moving the pressed direction.
I think it takes time for java to understand that the pressed button is HOLD. Any idea how I can make it move continuously instantly?
Upvotes: 2
Views: 1593
Reputation: 168845
Any idea how I can make it move continuously instantly?
KeyListener
to the component.keyPressed(KeyEvent)
keyReleased(KeyEvent)
Upvotes: 1