I've added it so when I press x it starts the animation but whenever I press it while moving the all of the animations just stop and it's like the sprite is frozen.
GreenfootImage[] attackAnim = { Attack1, Attack2, Attack3, Attack4, Attack5 };GreenfootImage[] currentAnim = stillRightAnim;
setImage(currentAnim[0]);
imageCounter = (++imageCounter)%(2*currentAnim.length); // make '2' higher to slow down animation
int direction = 0; // assume not moving
if (Greenfoot.isKeyDown("a")) direction--; // tend left
if (Greenfoot.isKeyDown("d")) direction++; // tend right
move(direction*3);if (Greenfoot.isKeyDown("x"))
{
if (currentAnim != attackLeftAnim && currentAnim != attackRightAnim) // new attack
{
if (direction != 0) // direction by key
currentAnim = direction == 1 ? attackRightAnim : attackLeftAnim;
else // no key -- direction by current animation
currentAnim = (currentAnim == stlllRightAnim || currentAnim == movingRightAnim) ? attackRightAnim : attackLeftAnim;
imageCounter = 0;
}
else // continued atttack
{
if (currentAnim == attackLeftAnim && direction == 1) // change to facing right
{
currentAnim = attackRightAnim;
setImage(currentAnim[imageCounter/(2*currentAnim.length)]);
}
if (currentAnim == attackRightAnim && direction == -1) / change to facing left
{
currentAnim = attackLeftAnim;
setImage(currentAnim[imageCounter/(2*currentAnim.length)]);
}
}
}
else // not attacking
{
if ((currentAnim == attackLeftAnim || currentAnim == attackRightAnim) && imageCounter == 0) // end attack
{
if (direction == 0) currentAnim = currrentAnim == attackLeft ? stillLeftAnim : stillRightAnim;
else currentAnim = direction == 1 ? movingRightAnim : movingLeftAnim;
}
else if (direction == 0 && (currentAnim == movingRightAnim || currentAnim == movingLeftAnim) // stopping
{
currentAnim = currentAnim == movingRightAnim ? stillRightAnim : stillLeftAnim;
imageCounter = 0;
}
else if (direction == 1 && currentAnim != movingRightAnim || direction == -1 && currentAnim != movingLeftAnim) // start moving or change directions
{
currentAnim = direction == 1 ? movingRightAnim : movingLeftAnim;
imageCounter = 0;
}
}
if (imageCount%(2*currentAnim.length) == 0) setImage(currentAnim[imageCount/(2*currentAnim.length)]);