To create an associative array using an Object constructor:
1.
Create a new Flash document, and save it as assocArray.fla.
2.
Add the following ActionScript to Frame 1 of the Timeline:
var monitorInfo:Object = {type:"Flat Panel", resolution:"1600 x 1200"};
trace(monitorInfo["type"] + ", " + monitorInfo["resolution"]);
This code creates an associative array called
initialize the array with two key/value pairs.
If you do not need to initialize the array at declaration time, you can use the Object
constructor to create the array:
var monitorInfo:Object = new Object();
3.
Select Control > Test Movie.
The Output panel displays the following text:
Flat Panel, 1600 x 1200
4.
Add the following ActionScript to Frame 1 of the Timeline, following the code you entered
previously:
monitorInfo["aspectRatio"] = "16:10";
monitorInfo.colors = "16.7 million";
trace(monitorInfo["aspectRatio"] + ", " + monitorInfo.colors);
After you use using either an object literal or the Object class constructor to create the
array, you can add new values to the array using either the bracket operator (
operator (
), as demonstrated in this code. The code you just typed adds two new values
.
to
monitorInfo
5.
Select Control > Test Movie.
The Output panel displays the following text:
16:10, 16.7 million
Note that a key can contain a space character. This is possible with the bracket operator,
but generates an error if you attempt this with the dot operator. Using spaces in your key
names is not recommended. For more information on bracket operators and dot
operators, see
"About operators" on page
code, see
"Formatting ActionScript syntax" on page
174
Syntax and Language Fundamentals
array.
, and uses an object literal to
monitorInfo
176. For more information on well-formatted
764.
) or the dot
[]
Need help?
Do you have a question about the FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH and is the answer not in the manual?