I tried this but it did not work.
I have to run an animation when the key is pressed.
My code:
case KeyEvent.VK_RIGHT:
while(KeyEvent.KEY_PRESSED){
estado = ANDA0;
posX += 10;
}
estado = PARADO;
break;
I tried this but it did not work.
I have to run an animation when the key is pressed.
case KeyEvent.VK_RIGHT:
while(KeyEvent.KEY_PRESSED){
estado = ANDA0;
posX += 10;
}
estado = PARADO;
break;
Your while loop will completely overwhelm the GUI event thread, preventing any actions from occurring, including animation actions and key reading events. Instead, I suggest that you:
while (true)
loop.For example, please have a look at this answer and example code of mine
Something like this using the KEY_RELEASED
event should do the trick:
case KeyEvent.VK_RIGHT:
if (KeyEvent.getID() == KeyEvent.KEY_PRESSED)
bool = true;
while(bool){
estado = ANDA0;
posX += 10;
if (KeyEvent.getID() == KeyEvent.KEY_RELEASED)
bool = false;
}
estado = PARADO;
break;