admin管理员组

文章数量:1434285

In a specific section of my code, the animation for a sprite stops at frame 1 when I run this.sprite.anims.play("key", true).

I have ran animations in other sections of my code and they have ran perfectly fine. However, for some reason, in this section, it is stopping at frame one. Here is the section:

In the update() function:


if (this.TIMER_DONE) {
  this.zeph.anims.play("walk_zeph-left", true)
  this.zeph.x += this.zeph_x_vel
  this.zeph_speech.visible = false
  this.t.setText("")
  if (this.zeph.x == 352) {
    this.zeph_x_vel = 0
    this.zeph.anims.play("walk_zeph-front", true)
    }
  }  

Basically, I have a timer. When the timer is done, I want the sprite to walk until it hits a specific coordinate. That part works perfectly fine. And then, when it does hit that coordinate, I want it to stop walking by setting the this.zeph_x_vel equal to 0. That part works fine too.

The only part that isn't working is the this.zeph.anims.play() part. It is playing the animation, but only the first frame. I have checked this with the other animations (ie, with the other keys), and the same thing is happening.

Here is my animation set_up:

this.anims.create({
            key: 'walk_zeph-front',
            frames: this.anims.generateFrameNumbers('zeph-front', {start: 0, end: 3}),
            frameRate: 8,
            repeat: -1
        })

I don't get any errors in the console, so, I'm not exactly sure what's happening. Any help would be greatly appreciated.

In a specific section of my code, the animation for a sprite stops at frame 1 when I run this.sprite.anims.play("key", true).

I have ran animations in other sections of my code and they have ran perfectly fine. However, for some reason, in this section, it is stopping at frame one. Here is the section:

In the update() function:


if (this.TIMER_DONE) {
  this.zeph.anims.play("walk_zeph-left", true)
  this.zeph.x += this.zeph_x_vel
  this.zeph_speech.visible = false
  this.t.setText("")
  if (this.zeph.x == 352) {
    this.zeph_x_vel = 0
    this.zeph.anims.play("walk_zeph-front", true)
    }
  }  

Basically, I have a timer. When the timer is done, I want the sprite to walk until it hits a specific coordinate. That part works perfectly fine. And then, when it does hit that coordinate, I want it to stop walking by setting the this.zeph_x_vel equal to 0. That part works fine too.

The only part that isn't working is the this.zeph.anims.play() part. It is playing the animation, but only the first frame. I have checked this with the other animations (ie, with the other keys), and the same thing is happening.

Here is my animation set_up:

this.anims.create({
            key: 'walk_zeph-front',
            frames: this.anims.generateFrameNumbers('zeph-front', {start: 0, end: 3}),
            frameRate: 8,
            repeat: -1
        })

I don't get any errors in the console, so, I'm not exactly sure what's happening. Any help would be greatly appreciated.

Share Improve this question asked Mar 17 at 22:01 Sereen TalebSereen Taleb 917 bronze badges 2
  • Did my answer solve your question, or did I miss something? If it help/solved your question, please consider accepting and/or upvoting my answer. To mark the question as solved. – winner_joiner Commented Mar 19 at 8:42
  • Yeah, your answer was correct. I actually managed to solve this myself a couple of days after posting this question, but you're answer correctly, and clearly, explained what was wrong. Sorry for the late upvote, I had a busy couple of days. – Sereen Taleb Commented Mar 20 at 22:24
Add a comment  | 

1 Answer 1

Reset to default 1

I assume the animation is stuck on the first frame of walk_zeph-front, if this is a problem of your nested if statement. First you are setting the animation to walk_zeph-left and that to walk_zeph-front, so it will play left (so fast you want be able to see it) and that front, with that it will always start at the first frame of front.

A Solution would be to extract the inner if - statement

if (this.TIMER_DONE && this.zeph.x == 352) {
  this.zeph_x_vel = 0
  this.zeph.anims.play("walk_zeph-front", true)
  //... add other lines that are needed
} else if (this.TIMER_DONE) {
  this.zeph.anims.play("walk_zeph-left", true)
  this.zeph.x += this.zeph_x_vel
  this.zeph_speech.visible = false
  this.t.setText("")
}  

本文标签: javascriptAnimation stops at frame 1 in Phaser 3Stack Overflow