Working with Arrays

Variables and for statements

Let's see if we can change that hard number in the balls index into a into a variable called i. Again, we can re-use that variable name because our original "i" is part of the init function:

In the example above, i = 1, so our second ball should be at Y = 50:

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

Get Adobe Flash player

Great! Now let's move all the balls with a for statement. We already have our i variable declared, so it should be a piece of cake:

Test it now.... all five of the balls should be at Y = 50:

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

Get Adobe Flash player

Don't forget to clean up that code! Tab over the two statements between the curly braces: