首页 > 解决方案 > 当精灵的运动在 Phaser 3 中停止时如何停止动画

问题描述

发布这个来分享我是如何解决这个问题的。我到处搜索,似乎找不到有效的解决方案。

问题(我的版本) - 我有一个精灵将成为我游戏中的玩家。我有一个向左奔跑和向右奔跑的动画。这些动画被设置为循环播放,因此当他们的速度增加时,玩家会看起来好像他们正在运行。我希望玩家的动画在他们停止移动后停止

标签: javascriptanimationphaser-framework

解决方案


我的实现

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();
            }
        });
}

格式有点差。几乎可以肯定,有一种稍微快一点的方法可以做到这一点。但这是我现在的实现,我只是想分享一下,以防其他人遇到同样的问题!随意评论更好的解决方案,或任何问题


推荐阅读