如何在Phaser 3中停止精灵运动时停止动画。

3

在此分享我是如何解决这个问题的。我已经搜索了很多地方,但好像找不到可行的解决方案。

问题是(我的版本)- 我有一个精灵,在我的游戏中将成为我的玩家。我有左跑和右跑两种动画。这些动画设置为循环,因此当玩家速度增加时,玩家看起来像在奔跑。我想让玩家停止移动后停止动画

3个回答

2

我的实现

update() {

        //If right arrowkey is pressed
        if (this.arrowKeys.right._justDown) {
            //Move player
            this.hero.setVelocityX(5);
            //Play your animation animation
            this.hero.anims.play('yourAnim', true);
        };

        //This will run every time the animation "repeats", meaning it has gone through all of its frames. Make sure your animation is set to loop!

        this.hero.on("animationupdate", () => {
            //If the player has stopped moving
            if (this.hero.body.velocity.x < 0.5 && this.hero.body.velocity.x > -0.5) {
                //Stop the animation
                this.hero.anims.stop();
            }
        });
}

格式有点不好。几乎肯定有一种稍微更快的方法来完成这个任务。但是这是我现在的实现方法,我只是想分享一下,以防其他人遇到同样的问题!欢迎评论提出更好的解决方案或任何问题。


1

我曾经遇到过这样的问题,也许你已经不需要解决方案了,但是其他人可能还需要。我使用了 JustDown 来启动动画,使用 JustUp 到达我的空闲状态。在 update 中的代码:

 if(Phaser.Input.Keyboard.JustDown(downKey)){
        gamePiece.anims.play("down");
 }
 if(Phaser.Input.Keyboard.JustUp(downKey)){
    gamePiece.anims.play("spin");
 }

在您的情况下,它会停止动画而不是空闲动画。

0
我认为你正在寻找 .pause() 方法。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接