Working with Arrays

Create the init Function

We have the makings of a good set of starting instructions for our program: We are creating a series of balls and arranging them on the stage. Where we go from here is a separate matter. For now, let's encapsulate all of our starting statements (except for our global variable balls)in a function commonly called init (short for initialize):

Now you, or any other programmer, can have a quick glance at your code and now exactly what you intend to run at the start of the program: Everything inside the init function!

We need to call the init function, or it will never run. It's common practice to put that statement near the top of our code, after we declare our key variables:

Test your work. It should run exactly as before:

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

Get Adobe Flash player

Finally, we need to clean up that code! Every line between the left and right braces needs to be tabbed over. This means the markup inside the for loop gets an additional tab:

That looks professional!