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

发布此信息以分享我如何解决此问题。我到处搜索,似乎找不到有效的解决方案。

问题(我的版本)-我有一个精灵,它将成为我游戏中的玩家。我既有向左奔跑的动画,也有向右奔跑的动画。这些动画设置为循环播放,因此当其速度提高时,播放器将看起来好像它们在奔跑。 我希望播放器停止移动后停止播放动画

moon5422 回答:当精灵的运动在Phaser 3中停止时如何停止动画

我的实现

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

格式化有点不好。而且几乎可以肯定有一种更快的方法。但这是我目前的实现,我只是想分享一下,以防其他人遇到相同的问题!随时以更好的解决方案或任何问题发表评论

,

我遇到这样的问题,您可能不再需要解决方案,但是其他人可能需要解决方案。我使用JustDown启动动画,并使用JustUp进入空闲状态。更新中的代码:

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

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

本文链接:https://www.f2er.com/3099116.html

大家都在问