- Code: Select all
for(i=0; i<10; i++){
do stuff;
}
i=0; - initializes the loop at 0. Sometimes i use i=1 because 0 means nothing is there (my first creep of each level will use slot 1, the second slot 2, etc), but usually this starts at 0.
i<10; - if the 'if' part of the statement. The loop will execute as long as this is true. So in my above loop, it will run ten times, the first being 0 and the last being 9. On the tenth loop, i will equal 10 and the for will no longer be true, and it will stop.
i++ - first, notice there is no ; at the end of this. second, this is what i will do each time the loop runs.
for instance, we can run it in reverse:
for (i=10; i>0; i--){
do stuff;}
So a great example is for an actor shooting a spread shot.
for(i=0; i<power; i++){
CreateActor("bullet", "bullet00", "no parent", "no path", 10*i, 0, false);
}
Now each bullet will appear 10 pixels over from the last one. This is great, because you can use i for all sorts of things. Since i use creator.eangle for the bullets angle, i'll use this:
eangle=120-(i*10);
Now if the power is 6, bullet one appears at 120 degrees, bullet 2 at 110 degrees, etc, with bullet 6 being at 60 degrees.
U can use I for all sorts of great stuff!