关于while循环

时间:2013-12-04 05:53:39

标签: javascript craftyjs

下面的代码只是'挂断'我的游戏,我正在使用craftyjs的javascript。我认为我正在做非常的noob错误导致无限循环..

var trigger_de_movement=true;
pl.animate('PlayerMovingLeft', 1, 1); //this function starts animation named PlayerMovingLeft for 1 frame and repeates 1 time..
while (trigger_de_movement) {/*if animation is not playing, type time and go out and stop the loop*/
    if (!pl.isPlaying('PlayerMovingLeft')) {//checks if animation is playing for pl(ayer)
        var d=new Date();
        putMessage(d.getSeconds()+"."+d.getMilliseconds()); //this function puts message
        trigger_de_movement=false; //to make while loop not run forever
    }
}

1 个答案:

答案 0 :(得分:0)

问题是,

if(!pl.isPlaying('PlayerMovingLeft')) 

如果If statement失败,那么循环将是无限的,因为变量trigger_de_movement一直为真,直到if语句变为真。如果是假,则循环是无限的。最好更改while to If并检查发生了什么