About while loop

81 views Asked by At

The following code just 'hangs up' my game, I am using javascript with craftyjs .. I think I am doing very noob mistake which is resulting into infinite loop ..

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

There are 1 answers

1
Linga On

The problem is,

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

If the If statement fails, then the loop will be infinite, because the variable trigger_de_movement is true always until the if statement becomes true. If it is false, the loop is infinite. So better you change your while to If and check what happening