Ah, but it is state-based. When the key is pressed I start an animation state, I just did not include all in the pseudo code to simplify it a bit
Try my approach: Same code applies to the other states. This is how my run left animation code is structured in my rpg.
public void keyPressed(KeyEvent e) {
if(KeyEvent.VK_LEFT == e.getKeyCode() )
{
isLeftPressed = true;
isLeftReleased = false;
}
}
public void update()
{
if(isLeftPressed)
{
direction = Direction.LEFT;
}
}
if(isLeftPressed || isRightPressed || isUpPressed || isDownPressed)
{
state = ActionState.RUNNING;
}
if(state == ActionState.RUNNING && direction == Direction.LEFT)
{
soraRunLeftAnimation.incrementFrame();
if(isLeftReleased || soraRunLeftAnimation.isLastAnimFrame())
{
state = ActionState.IDLE;
soraRunLeftAnimation.resetAnim();
}
}
}
public void draw(Graphics2D g2D)
{
if(state == ActionState.RUNNING && direction == Direction.LEFT && !isRightPressed )
{
soraRunLeftAnimation.draw(g2D);
}
}