About arrays 165
To use arrays in your code:
1. Create a new Flash document, and save it as basicArrays.fla.
2. Add the following ActionScript to Frame 1 of the Timeline:
// define a new array
var myArr:Array = new Array();
// define values at two indexes
myArr[1] = "value1";
myArr[0] = "value0";
// iterate over the items in the array
var i:String;
for (i in myArr) {
// trace the key/value pairs
trace("key: " + i + ", value: " + myArr[i]);
}
In the first line of ActionScript, you define a new array to hold the values. Then, you
define data (
value0 and value1) at two indexes of the array. You use a for..in loop to
iterate over each of the items in that array and display the key/value pairs in the Output
panel using a trace statement.
3. Select Control > Test Movie to test your code.
The following text is displayed in the Output panel:
key: 0, value: value0
key: 1, value: value1
For more information on for..in loops, see “Using for..in loops” on page 158.
For information on how to create different kinds of arrays, see the following sections:
■ “Creating indexed arrays” on page 168
■ “Creating multidimensional arrays” on page 169
■ “Creating associative arrays” on page 172
You can find a sample source file, array.fla, in the Samples folder on your hard disk. This
sample illustrates array manipulation using ActionScript. The code in the sample creates an
array and sorts, adds, and removes items of two List components. Find the sample file in the
following directories:
■ In Windows, browse to boot drive\Program Files\Macromedia\Flash 8\Samples and
Tutorials\Samples\ActionScript\Arrays.
■ On the Macintosh, browse to Macintosh HD/Applications/Macromedia Flash 8/Samples
and Tutorials/Samples/ActionScript/Arrays.