Loading an Array

Loading an Array

Flash CS4 ActionScript 3.0 Tutorial


How to Load Data into an Array Variable



Arrays are used to organize large amounts of data. Arrays can be described as multi-dimensional variables that can hold several values. There are multiple ways to assign array values.

In this example we will demonstrate two ways to load an Array variable with data.

For more information on Arrays, see topics, Arrays, Accessing Array Data and Manipulating Array Contents.


Step One

Open a new Flash document and name the default layer Actions.

Step Two

Option double-click (PC - Alt double-click) frame one of the Actions layer.

Step Three

Copy/Paste the following code to the ActionScript panel.

var boxColor01:Array = new Array("Red01", "Green01", "Blue01");

var boxColor02:Array = new Array;

boxColor02[0]= "Red02";

boxColor02[1]= "Green02";

boxColor02[2]= "Blue02";

trace(boxColor01[0] + boxColor01[1] + boxColor01[2]);

trace(boxColor02[0] + boxColor02[1] + boxColor02[2]);

Step Four

Publish your movie by pressing Cmd/Return (PC - Ctrl/Enter). The values of each Array is displayed in the Output panel. The top line shows the boxColor01 contents and the second line displays the boxColor02 Array.

Dissecting the Code

The first line of the code at Step Three establishes a new variable named boxColor01. For the boxColor01 variable, Red01 is loaded in slot [0], Green01 is in slot [1] and Blue01 is loaded in slot [2].

The next line of the code establishes a new variable named boxColor02. For the boxColor02 variable, Red02 is loaded in slot [0], Green02 is in slot [1] and Blue02 is loaded in slot [2].

To see the contents of both Arrays, we added a trace statement. Both methods work when loading an Array. The first method is more compact.