I want to start my for loop at a certain number, and make it loop. Like this:
5, 6, 7, 8, 9, 10, 0, 1, 2, 3, 4
If the maximum number is 10 and it starts at 5.
The reason is because I want to loop through every tile in my game in a certain order (top to down, then go right).
I have tried the following but it skips one number:
for(var i = position[0]+1; i != position[0]; i++){
for(var j = position[1]+1; j != position[1]; j++){
if(j > 11){
j = 0;
}
if(i > 23){
i = 0;
}
if(levelMap[i][j] == type){
return position;
}
trace("j" + j);
}
trace(i);
}
Thanks
If you want a sequence like
5, 6, 7, 8, 9, 10, 0, 1, 2, 3, 4(where the starting point can be any number in that set), you don’t need fancy loop constructs.You can just use a normal loop with the modulo operator, something like (pseudo-code since one of the tags was
language-agnostic):This will iterate
use_ifrom 5 to 4, cycling back to 0 after 10.