I am making a platformer like game that allows the player to move left and right, and allows the player to attack. The problem is that when the player is moving left or right (by holding the left or right buttons) and then attack using the down button (while holding the left or right button), the attack animation does not stop and pauses at the last frame and the player continues to move left or right without the walking animation.
this is the code for walking left or right
if (this.cursors.left.isDown)
{
player.setFlipX(false);
player.setVelocityX(-walkVelocity);
if (direction != 'left')
{
player.anims.play('playerWalk');
}
direction = 'left';
trueDirection = 'left';
}
else if (this.cursors.right.isDown)
{
player.setFlipX(true);
player.setVelocityX(walkVelocity);
if (direction != 'right')
{
player.anims.play('playerWalk');
}
direction = 'right';
trueDirection = 'right';
}
else if (!attacking)
{
if (direction != 'none')
{
player.anims.play('playerIdle');
}
direction = 'none';
//ignore the grappling part, this is another unrelated mechanic in the game
//for this scenario, assume the grappling variable is equal to false
if (grappling == false)
{
player.setVelocityX(0);
}
}
this is the code for attacking
if (this.cursors.down.isDown && !attacking)
{
//the player sword animation shows the player striking a sword
player.anims.play('playerSword');
attacking = true;
grappling = false;
// Listen for the animation to finish
player.on('animationcomplete', function (animation, frame) {
attacking = false;
});
}
all of the code above was in the update function