I have an animation, which usually is supposed to play 3 times, thus the config currently says repeat: 2
. However, under a certain condition (the player stops dragging an element) I want the animation to finish and stop without repeating.
By that I mean I don’t want it to stop right at the frame it is at the second, but I want it to finish playing to the last frame I assigned in frames
and then stop before it repeats.
How would I be able to do this?
JavaScript
x
32
32
1
// ANIMATION CREATED HERE
2
this.anims.create({
3
key: "nom",
4
frameRate: 12,
5
frames: this.anims.generateFrameNames("chara", {
6
prefix: "nom_000",
7
start: 0,
8
end: 4}),
9
repeat: 2,
10
});
11
12
this.input.on('drag', (activePointer, gameObject, dragX, dragY) => {
13
gameObject.x = dragX;
14
gameObject.y = dragY;
15
16
if(Phaser.Geom.Intersects.RectangleToRectangle(gameObject.getBounds(), character.mouth.getBounds())) {
17
gameState.snack_cursor.play("fries_cursor", true);
18
19
// ANIMATION PLAYED HERE
20
character.sprite.play("nom", true);
21
}
22
});
23
24
this.input.on('dragend', (pointer, gameObject, dragX, dragY) => {
25
if (gameObject.anims.isPlaying)
26
gameObject.anims.stop();
27
28
// ANIMATION STOPS HERE, BUT CURRENTLY AT WHATEVER FRAME IT'S AT
29
if (character.sprite.anims.isPlaying)
30
character.sprite.anims.stop();
31
});
32
Advertisement
Answer
You can use the function stopAfterRepeat
(documentation).
Just call it with the parameter 0
, and the current animation will be finished, before the animation is stopped. I think this is what you are after.
JavaScript
1
4
1
2
character.sprite.anims.stopAfterRepeat(0);
3
4