简体   繁体   English

Phaser 3 上的 moveToObject 精灵动画

[英]moveToObject sprite animation on Phaser 3

I have a character that has a click to point movement.我有一个角色可以点击指向移动。 I'm using MoveToObject I want to implement an animation to the sprite based on the direction it's moving similar to this code snippet below我正在使用 MoveToObject 我想根据精灵移动的方向为精灵实现动画,类似于下面的代码片段

if (this.cursors.left.isDown)
{
    this.player.setVelocity(-speed, 0)
    this.player.play('left-walk', true)
}
else if (this.cursors.right.isDown)
{
    this.player.setVelocity(speed, 0)
    this.player.play('right-walk', true)
}
else if (this.cursors.up.isDown)
{
    this.player.setVelocity(0, -speed)
    this.player.play('up-walk', true)
}
else if (this.cursors.down.isDown)
{
    this.player.setVelocity(0, speed)
    this.player.play('down-walk', true)
}

I'm guessing getting the current angle of the sprite but I can't seem to make it work.我猜想得到精灵的当前角度,但我似乎无法让它工作。 Thank you谢谢

I have solved the following problem.我已经解决了以下问题。 Thank you!谢谢!

 angles = Phaser.Math.Angle.BetweenPoints(avatar, target)
        var newAngles = Phaser.Math.RadToDeg(angles)
        console.log(newAngles)

        if (avatar.body.speed > 0)
        {
            if(newAngles <= -45 && newAngles >= -135) {
            avatar.anims.play('back', true)
            } else if (newAngles <= 45 && newAngles >= -45) {
                avatar.flipX = true
                avatar.anims.play('side', true)
            } else if (newAngles <= 135 && newAngles >= 45) {
                avatar.anims.play('front', true)
            } else if (newAngles <= 225 && newAngles >= 135) {
                avatar.flipX = false
                avatar.anims.play('side', true)
            }
            
            
            
            if (distance < 4)
            {
                avatar.anims.pause()
                avatar.body.reset(target.x, target.y);                
            }
        }       

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM