如何在 Phaser 3 中停止动画循环,但不停止动画中途?

发布于 2025-01-12 10:22:43 字数 1145 浏览 0 评论 0原文

我有一个动画,通常应该播放 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝不放开 2025-01-19 10:22:43

您可以使用函数 stopAfterRepeat (

 ...
 character.sprite.anims.stopAfterRepeat(0);
 ...

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.

 ...
 character.sprite.anims.stopAfterRepeat(0);
 ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文