KeyEvents

Right now I'm using a key listener to get any key events on an applet. If the up arrow is pressed an image moves up, if the left arrow is pressed the image moves left, etc, but there is a delay between the time the key is pressed and the image moves. If I just tap the key the image moves at the correct time, but if the key is held down the delay happens. Any way of fixing this?

Also, when I am holding a key, the up arrow for example, and then press the left arrow my image stops moving up. The left key interrupts the up key. Is there a way to fix this also?

Thanks,

Brian

[602 byte] By [untwisteda] at [2007-9-29 11:40:28]
# 1

Aha! I've solved both problems with one solution.Here's my code for anyone who has had this problem:

public void keyPressed(KeyEvent e) {

keyVal = e.getKeyCode();

switch(keyVal) {

case 38 : upPressed = true;//Up

break;

case 40 : downPressed = true;//Down

break;

case 37 : leftPressed = true;//Left

break;

case 39 : rightPressed = true;//Right

}

}

public void keyReleased(KeyEvent e) {

keyVal = e.getKeyCode();

switch(keyVal) {

case 38 : upPressed = false;//Up

break;

case 40 : downPressed = false;//Down

break;

case 37 : leftPressed = false;//Left

break;

case 39 : rightPressed = false;//Right

break;

}

}

public void processKeyEvents() {

if(upPressed == true) { y-= 5; }

if(downPressed == true) { y+= 5; }

if(leftPressed == true) { x-= 5; }

if(rightPressed == true) { x+= 5; }

}

x & y are integers that hold the x & y coordinates for my image (a space ship).

untwisteda at 2007-7-15 1:12:27 > top of Java-index,Other Topics,Java Game Development...