Creating and Initializing Arrays

[ LiB ]

Okay, you know how to reference an arrayso how would you create one? There are a few ways to create arrays. Let's look at the array constructor first.

 myArray = new Array(); 

Here you see the new command along with the array constructor. The constructor is nothing more than a special built-in function. The statement new Array() , in this case, creates a new array that can be assigned to an alias that can later be initialized and indexed. That's all there is to it.

There is no predefined number of elements in the array just created. This array will actually create the elements as you assign them to the indexed elementthis is called a dynamic array . Take a look at an example:

 myArray[0] = 42; myArray[1] = 63; myArray[2] = 34; 

The first three elements to this array were initialized to 42, 63, and 34. You could handle these just like I explained in the last section. You could even use each of the elements as a variable to assign or to manipulate its contents. For instance, you can do this:

 tempVar = myArray[2]; 

You should do this if you don't want to operate directly on the array's contents because this way, you could work on tempVar without worries. This assignment will definitely come in handy if you're about to enter a section of code that specifically works on this one elementinstead of writing the reference out all the time you can simply refer to the second alias.

NOTE

TIP

If you see a way to simplify your code, don't hesitate to do so. I per sonally go for code flexibility.What I mean by this is you should try to plan for the future. If I believe that anything might be changed or modi fied, I then program in such a way that the code is easily upgradeable. As I see it, the code is never finished and can forever be tweaked. I guess that's why there are so many ver sions of programs out there.

Creating an array like this is fine, but what if you have a predetermined amount of elements you want to work with? Luckily, there is a way you can tell ActionScript to create a certain number of elements that won't immediately be initializedby stating the number within the constructor's parentheses, like this:

 myArray = new Array(50); 

This way, you can automate the initialization of this array with a loop or some other program flow statement. Just to throw some examples of this to you, say you wanted to initialize 25 different enemies in 25 different screen positions. You could easily put this array in a loop that initializes pairs of random x and y values in the array. Doing so will cause your enemies to be in different screen positions just as you intended.

Another quick example would be creating a number list that will be stored and saved for later uselet's say, the numbers from 1 to 365. Once again, the loop can come to the rescue and fill these values into the array with no problem.

It is possible to skip the initialization of elements in between other initialized ones. Allow me to restate that: It is possible , but not recommended , to skip the initialization of certain elements. This type of array is called a sparse array . See Figure 5.2.

Figure 5.2. Visualizing a sparse array

graphic/05fig02.gif


NOTE

CAUTION

As I said, it's not a good idea to use sparse arrays. Using an array with gaps in it, or even misreferencing the array can cause unpredicted results in your program. The funny thing about a sparse array is that Flash will not complain. If you are assigning a value to an element that is not initialized, Flash will simply create the element and the program will move on.This isn't good, especially if that wasn't your original intention .

So is there another way to initialize the array if we already know what the values are? Of course there is! There are a couple of ways, actually. Let me show you how to initialize the array using the current notation of our constructor (you'll see what I mean shortly). Check this out:

 myArray = new Array("Lewis", "Eddie", "David", "Jerron"); 

If you type this line in, you create a new array with four elements. These elements were initialized to four names : Lewis, Eddie, David, and Jerron. This way to initialize an array is fine for a few values, but it would be ridiculous for anything past 30 valuesat least you have the choice to initialize them in this way.

Just because you initialize an array through the constructor doesn't mean that you can't add elements to the array. If the fourth element was your last element, you can easily create the fifth element by assigning something to myArray[4] . It's that easy.

Here's another, shorter way to rewrite the statement new Array() :

 myArray = []; 

This also declares an array with an unknown number of elements. The two ways of declaring an array can be used interchangeably.

What would you do if you wanted to assign some values to the array? Easy enoughjust write in the values in the bracketsif you have more than one value, separate them with a comma like this:

 myArray = [25, 24, 35, 18, 58]; 

Cool, now you know how to create and initialize arrays. Don't be surprised when you see both types of notations.

[ LiB ]


Game Development with ActionScript
Game Development with ActionScript
ISBN: 1592001106
EAN: 2147483647
Year: 2004
Pages: 162
Authors: Lewis Moronta

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net