2.
Type the following ActionScript on Frame 1 of the Timeline:
// Define the object to use as an associative array.
var someObj:Object = new Object();
// Define a series of properties.
someObj.myShape = "Rectangle";
someObj.myW = 480;
someObj.myH = 360;
someObj.myX = 100;
someObj.myY = 200;
someObj.myAlpha = 72;
someObj.myColor = 0xDFDFDF;
// Display a property using dot operator and array access syntax.
trace(someObj.myAlpha); // 72
trace(someObj["myAlpha"]); // 72
The first line of ActionScript defines a new object (
associative array. Following this, you define a series of properties in
display a property that you select using both dot operator and array access syntax.
You can access variables in an associative array using two different methods: dot
syntax (
someObj.myColor
3.
Select Control > Test Movie to test your ActionScript.
The Output panel displays the number
that you traced.
There are two ways to create associative arrays in ActionScript 2.0:
Use an Object constructor
Use an Array constructor
Both of these ways are demonstrated in upcoming examples.
The previous example used an Object constructor to create an associative array.
If you use the Object constructor to create an associative array, you can take advantage of
initializing your array with an object literal. An instance of the Object class, also called a
generic object, is functionally identical to an associative array. In fact, Object instances are
essentially associative arrays. You might use associative arrays for dictionary-like functionality,
when it's more convenient to have string keys rather than numerical indices. Each property
name of the generic object serves as the key that provides access to a stored value. For more
information on literals, see
Chapter 7, "Classes," on page
) and array syntax (
twice, which represents both of the alpha levels
72
"About literals" on page
225.
) that you use as the
someObj
someObj
).
someObj['myColor']
130. For more information on classes, see
. Finally, you
About arrays
173
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?