如何在 Phaser 3 中停止动画循环,但不停止动画中途?
我有一个动画,通常应该播放 3 次,因此当前配置为 repeat: 2
。但是,在某种条件下(玩家停止拖动元素)我希望动画完成并停止而不重复。
我的意思是我不希望它在第二帧处停止,但我希望它完成播放到我在 frames
中指定的最后一帧,然后在重复之前停止。
我怎样才能做到这一点?
// ANIMATION CREATED HERE
this.anims.create({
key: "nom",
frameRate: 12,
frames: this.anims.generateFrameNames("chara", {
prefix: "nom_000",
start: 0,
end: 4}),
repeat: 2,
});
this.input.on('drag', (activePointer, gameObject, dragX, dragY) => {
gameObject.x = dragX;
gameObject.y = dragY;
if(Phaser.Geom.Intersects.RectangleToRectangle(gameObject.getBounds(), character.mouth.getBounds())) {
gameState.snack_cursor.play("fries_cursor", true);
// ANIMATION PLAYED HERE
character.sprite.play("nom", true);
}
});
this.input.on('dragend', (pointer, gameObject, dragX, dragY) => {
if (gameObject.anims.isPlaying)
gameObject.anims.stop();
// ANIMATION STOPS HERE, BUT CURRENTLY AT WHATEVER FRAME IT'S AT
if (character.sprite.anims.isPlaying)
character.sprite.anims.stop();
});
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?
// ANIMATION CREATED HERE
this.anims.create({
key: "nom",
frameRate: 12,
frames: this.anims.generateFrameNames("chara", {
prefix: "nom_000",
start: 0,
end: 4}),
repeat: 2,
});
this.input.on('drag', (activePointer, gameObject, dragX, dragY) => {
gameObject.x = dragX;
gameObject.y = dragY;
if(Phaser.Geom.Intersects.RectangleToRectangle(gameObject.getBounds(), character.mouth.getBounds())) {
gameState.snack_cursor.play("fries_cursor", true);
// ANIMATION PLAYED HERE
character.sprite.play("nom", true);
}
});
this.input.on('dragend', (pointer, gameObject, dragX, dragY) => {
if (gameObject.anims.isPlaying)
gameObject.anims.stop();
// ANIMATION STOPS HERE, BUT CURRENTLY AT WHATEVER FRAME IT'S AT
if (character.sprite.anims.isPlaying)
character.sprite.anims.stop();
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用函数
stopAfterRepeat
(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.