Creating a Loop

Position and Cleanup

Maybe it's working, maybe it's not. I'm not seeing any errors in the compiler, but it looks like I still have just one ball on the stage. The problem is all they are stacked all on top of one another at (100,150). Let's do something about that!

We need to differentiate the positing of these balls, and lucky for us, we have a variable that changes with each loop: i.

That's right: i starts at zero, then becomes 1, 2, 3, and 4 as the loop progresses. Let's add i to our X position:

Test it! If you look real close, you'll see the balls have spread out to the right:

Content on this page requires a newer version of Adobe Flash Player.

Get Adobe Flash player

OK, we have to look a little too close to see a change. One pixel isn't quite enough. Let's use some more math: Multiply i times 50 with each loop:

This is what our math looks like as we go through the loops... we use the equation 100+i*50 and solve for five different values of i:

100+i*50 = ?
100+0*50 = 100
100+1*50 = 150
100+2*50 = 200
100+3*50 = 250
100+4*50 = 300

Now we can see a real change. We finally found a use for Algebra:

Content on this page requires a newer version of Adobe Flash Player.

Get Adobe Flash player

Before we go any further, let's clean this up. The actions inside the left and right for loop braces should all be tabbed over. Again, this will not change the way your published project runs. However, this is standard coding practice. It will make your boss happy, your coworkers happy, and it will make YOU happy during those long, bleary-eyed ActionScript coding sessions: