This is my first time trying to use KeyListener and I just need it to move an image over 20 px every time I press the right arrow key. After 400px, it should move down to the next line vertically 'playery.' For Some reason it prints out the console output twice. I also just want to make sure my layout for everything is common and acceptable
addKeyListener (
new KeyListener() {
public void keyPressed(KeyEvent e) {
System.out.println("key pressed");
if (e.getKeyCode() == KeyEvent.VK_RIGHT){
playerx+=20;
System.out.println("playerx: "+playerx);
if (playerx>400){
playery+=20;
playerx=0;
}
repaint();
}
}
public void keyReleased(KeyEvent arg0) {
System.out.println("key released");
}
public void keyTyped(KeyEvent arg0) {
System.out.println("key typed");
}
}
);
This is my console output:
key pressed
playerx: 20
key pressed
playerx: 40
key released
key released
key released
For some reason, one key stroke replies with 2.