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.
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("")
}