Now that you know what an array is and what it does, let's discuss how to create one. There are several different ways to create an array; however, we will be dealing mainly with the new operator and the Array constructor to build our sample array.
When creating arrays using the new operator and the Array constructor, start by setting a variable. Then make the variable equal to the new operator combined with the Array constructor, followed by a set of parentheses and then a semicolon (to end the line of code). Here's an example:
Open a new movie, click the first frame of the main timeline, and open the Actions panel (F9).
Place the following code in your actions:
var my_array:Array = new Array();
You're done! You just created your first array, so let's take a look at it. Add the following trace function to your code:
Now test your movie by going to the toolbar and choosing Control > Test Movie. When you test your movie, an output window will open because of the trace function in the code. However, nothing appears in the window, as shown in Figure 10.1. That's because there is nothing in our array. Let's go back and add some data to the array.
Figure 10.1. An empty output window for your movie.
As mentioned earlier, each element in an array is labeled with the array's name and an integer that represents its position inside of the array. The first element in an array will always have an index of 0, followed by 1, and so on. Because you have already created the array, you will label the new elements manually. Under where you created the array, type the array name and then 0 in brackets, like this:
var my_array:Array = new Array(); my_array  = "fName"; my_array  = "lName"; trace(my_array); //output: fName, lName
Now that you have data in the array, you can continue to add elements. However, it's much easier to create the elements right in the beginning, so let's do that next. Also, notice this time that the output of the code is preceded by comment marks (//). As in previous chapters, these marks are merely to show the output of the code from within the ActionScript itself.
You will still be using the original code, but when you create the array this time, you will create it with data inside. Elements in an array can be of any data type, as discussed earlier. Let's use a couple strings to start with. When putting elements in an array when it is created, place them in the parentheses and separate them with commas, like this:
var my_array:Array = new Array("fName","lName"); trace(my_array); //output: fName, lName
Another way of creating an array with information doesn't involve the new operator or the Array constructor. You can simply set a variable equal to the elements you want in the array, but instead of putting them inside parentheses, place them between brackets, as shown here:
var my_array:Array = ["fName","lName"]; trace(my_array); //output: fName, lName
This code outputs the same as the other examples. However, just remember that when you do not use the new operator and the Array constructor, you must place the elements in brackets.
You can even put just one piece of data in the array when you create it, but make sure it is not an integer. Otherwise, some surprising results will happen. Let's take a look.
Use the same code as before but replace what is in the parentheses with the number 5 (note that the output of the code is shown within the code using comment marks):
var my_array:Array = new Array(5); trace(my_array); //output: undefined,undefined,undefined,undefined,undefined
When you test the movie, notice that it doesn't display the number 5 but instead displays 5 elements as undefined. This is because when you place only an integer in an array, it creates that many blank elements.
You can also store variables in arrays just like any other type of data, and the data stored in the variables will display in the array:
var myName:String = "David"; var myAge:Number = 25; var my_array:Array = new Array(myName, myAge); trace(my_array); //output: David, 25
But make note, when storing a variable in an array, it actually stores the data from that variable, and not the reference to the variable. Here is an example of this:
var myName:String = "David"; var myAge:Number = 25; var my_array:Array = new Array(myName, myAge); trace(my_array); myAge = 35; trace(my_array); //output: David, 25 // David, 25
You can see that although we changed the value of myAge, when we traced the array for the second time, the data was the same.
Besides variables, arrays can also hold other arrays (called nested arrays). Nested arrays are useful for holding multiple lists in one place. Just place the name of the array as an element, as you would a variable:
var myNames:Array = new Array("fName","lName"); var my_array:Array = new Array("age",myNames); trace(my_array); //output: age, fName, lName
The second array simply encompasses the first array. However, if you trace the last element in my_array, you will see that it doesn't separate the elements from myNames. Let's take a look at this:
var myNames:Array = new Array("fName","lName"); var my_array:Array = new Array("age",myNames); trace(my_array ); //output: fName, lName
As you can see, even though it appears that when we added myNames to my_array, the elements from myNames came over as one element. Just keep this in mind when you add arrays to arrays.