Home > OS >  How to play an animation only if there isn't one yet in Phaser?
How to play an animation only if there isn't one yet in Phaser?

Time:12-15

What I want to do is play a running animation on a sprite, every time a key is pressed

My code (extract) looks like this:


function update() {
  if (keys["w"].isDown) {
    avatar.setVelocityY(-250);
  }
  if (keys["d"].isDown) {
    avatar.setVelocityX(250);
  }
  if (keys["a"].isDown) {
    avatar.setVelocityX(-250);
  }
  if (keys["s"].isDown) {
    avatar.setVelocityY(250);
    avatar.play("run_front");
  }
}

The problem: When I hit s, only the first frame of the animation is played and the animation will only finish when I stop pressing it.

I think this is because the animation is overwritten every time I press s.

So how can I run the animation only if the one before isn't playing yet?

Thanks in advance

CodePudding user response:

I found the solution in using avatar.anims.isPlaying

So what I did is:

    if (!avatar.anims.isPlaying) avatar.anims.play("lazy");


  • Related