简体   繁体   English

Phaser3旋转动画

[英]Phaser3 Rotation Animation

I'm trying to create a simple animation effect of a pendulum swinging back and forth. 我正在尝试创建一个简单的来回摆动的动画效果。 I'm only using a single image sprite for it, and I've got the code below, but the animation only tilts to a 45 angle then stops completely. 我只使用一个图像精灵,下面有代码,但是动画仅倾斜到45度角,然后完全停止。 What might I be doing wrong? 我可能做错了什么?

    var title = new Phaser.Scene("GameTitle");
var pendulum;
var direction;
title.create = function(){
    pendulum = this.add.sprite(200, 0, 'titlePendulum').setOrigin(0.5, 0).setScale(1.8).setRotation(79);
    direction = "left";
};

title.update = function(){
    console.log(pendulum.angle.toFixed(0));
    swing(direction);
    if(pendulum.angle.toFixed(0) == 71){
        swing ("right");
    }
    if(pendulum.angle.toFixed(0) == -76){
        swing("left");
    }
};

function swing(dir){
    if(dir == "left"){
        if(pendulum.angle.toFixed(0) == 71){
            swing("right");
        }else{
            pendulum.angle +=1.5;
        }

    }else{
        if(pendulum.angle.toFixed(0) > -80){
            pendulum.angle -= 1.5;
        }
        if(pendulum.angle.toFixed(0) == -76){
            swing("left");
        }
    }
}

I noticed that you check if the pendulum.angle is larger than or exactly equal to 80 in two separate if statements. 我注意到您在两个单独的if语句中检查pendulum.angle是否大于或等于80。 However 80 is not divisible by 1.5, and the value of the angle could be .. 76, 77.5, 79, 80.5 .. and the condition pendulum.angle.toFixed(0) == 80 will never be true. 但是80不能被1.5整除,角度的值可能是.. 76, 77.5, 79, 80.5 ..并且条件pendulum.angle.toFixed(0) == 80将永远不成立。

Also, the name of the function parameter is dir but you check for a direction . 另外,函数参数的名称为dir但您需要检查direction

I'm pretty sure that is causing the problem. 我很确定这是导致问题的原因。 Even if that's not it, it's still better (more elegant and "cleaner") to use if-else when you are checking for a condition that can either be one thing or the other, instead of using two separate if statements. 即使不是那样,当您检查条件可以是一件事或另一件事时,而不是使用两个单独的if语句,使用if-else还是更好(更优雅,更“干净”)。

So change your code to something like this: 因此,将您的代码更改为如下所示:

function swing(dir) {
    if (dir == "left") { // check parameter, not the direction global var
        if (pendulum.angle.toFixed(0) < 80) {
            pendulum.angle +=1.5;
        } else {
            swing("right");
        }
    } else { // dir == "right"
        if (pendulum.angle.toFixed(0) > -80) {
            pendulum.angle -= 1.5;
        } else {
            swing("left");
        }
    }
}

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

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