Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

284
Views
How to play an animation only if there isn't one yet in Phaser?

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

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

I found the solution in using avatar.anims.isPlaying

So what I did is:

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


about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!