Working with Arrays

Access the Array

Each time we push a new myBall object to the array balls, it creates a new entry in the array index. Like much of computer programming, the numbering starts at 0. If we want to access our first ball object, we lookup balls[0]. If we want to access our third ball object, we lookup balls[2].

Let's make some stuff happen outside the init function. First, we create a variable we can use to reference our third ball object (balls[2]):

Notice that I named the variable myBall, a name we have already used! Usually, that would throw a big compiler error. The reason it doesn't in this case is because the "original" myBall is part of the init function. It's as if it belongs to that function.

OK, now let's set the Y position of myBall:

...and give it a test. The third ball in line should be at position (200,50):

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

Get Adobe Flash player