• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker Infinity While loop error?

I

Isabella Ava

Guest
Hi guys, i had checked again & again but i cannot understand why this code of mine make my game frozen when var oPlayer.elDownDistance = oPlayer.elLineAmount
Ummm >. < whyyyy? please!

Code:
var evKeyDown = keyboard_check(vk_down);
var evKeyUp = keyboard_check(vk_up);

var evSpd = (evKeyDown - evKeyUp) * 8;
if oPlayer.elDownDistance + evSpd >= oPlayer.elLineAmount {
   while( (oPlayer.elDownDistance + sign(evSpd) ) <= oPlayer.elLineAmount) {
       oPlayer.elDownDistance += sign(evSpd);
       y += sign(evSpd);
   }
   evSpd = 0;
}   
oPlayer.elDownDistance += evSpd;
y += evSpd;
 

samspade

Member
Hi guys, i had checked again & again but i cannot understand why this code of mine make my game frozen when var oPlayer.elDownDistance = oPlayer.elLineAmount
Ummm >. < whyyyy? please!

Code:
var evKeyDown = keyboard_check(vk_down);
var evKeyUp = keyboard_check(vk_up);

var evSpd = (evKeyDown - evKeyUp) * 8;
if oPlayer.elDownDistance + evSpd >= oPlayer.elLineAmount {
   while( (oPlayer.elDownDistance + sign(evSpd) ) <= oPlayer.elLineAmount) {
       oPlayer.elDownDistance += sign(evSpd);
       y += sign(evSpd);
   }
   evSpd = 0;
}  
oPlayer.elDownDistance += evSpd;
y += evSpd;
You haven't thought through all the edge cases. At first I missed it too. Here's the question, what happens if you're not pushing up or down?

If you're not pushing up or down, Spd is 0 and the sign of 0 is 0 and any number plus 0 is still that number. So if you ever made it into the while loop with Spd == 0 you'll be stuck forever. Which is possible if DownDistance and LineAmount are equal and Spd is 0. I think adding && (evSpd != 0) to the first if statement would be a solution here.
 
I

Isabella Ava

Guest
You haven't thought through all the edge cases. At first I missed it too. Here's the question, what happens if you're not pushing up or down?
Ops! That's totally a brilliant question = ) Thank you very much
 
Top