关于while循环

About while loop

本文关键字:循环 while 关于      更新时间:2024-03-06

下面的代码刚刚"挂断"了我的游戏,我正在使用带有craftyjs的javascript。。我想我犯了一个很小的错误,导致了无限循环。。

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
    }
}

问题是,

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

如果If statement失败,那么循环将是无限的,因为变量trigger_de_movement始终为true,直到If语句变为true。如果为false,则循环为无穷大。因此,最好更改while to If并检查发生了什么