I’m still having problems where my input handler for keyboard input is firing for no keys pressed. Even If I am holding down a key. I think windows is firing repeating pressed/released events. This causes my sprite to stop during some frames and move during others. Here is my code.
public void processKeyEvent(KeyEvent ke)
{
controls[ke.getKeyCode()&0xFF] = (ke.getID()==KeyEvent.KEY_RELEASED)?0:1 ;
}
private void updateInput()
{
// if(state == PLAY)
// {
if(controls[KeyEvent.VK_UP] == 1)
{
diry = -2;
dirx = 0;
hero.setDirection(Hero.UP);
hero.start();
}
else if(controls[KeyEvent.VK_DOWN] == 1)
{
diry = 2;
dirx = 0;
hero.setDirection(Hero.DOWN);
hero.start();
}
else if(controls[KeyEvent.VK_LEFT] == 1)
{
dirx = -2;
diry = 0;
hero.setDirection(Hero.LEFT);
hero.start();
}
else if(controls[KeyEvent.VK_RIGHT] == 1)
{
dirx = 2;
diry = 0;
hero.setDirection(Hero.RIGHT);
hero.start();
}
else
{
dirx = 0;
diry = 0;
// hero.stop();
}
if(controls[KeyEvent.VK_ESCAPE] == 1) running = false;
}
I call updateInput() every frame.
If I remove the block for the last else everything is smooth as glass. :-/