EasyManua.ls Logo

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH - Page 173

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
830 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
About arrays 173
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 (someObj) that you use as the
associative array. Following this, you define a series of properties in
someObj. Finally, you
display a property that you select using both dot operator and array access syntax.
3. Select Control > Test Movie to test your ActionScript.
The Output panel displays the number
72 twice, which represents both of the alpha levels
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.
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 its 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 About literals” on page 130. For more information on classes, see
Chapter 7, “Classes,” on page 225.
NOTE
You can access variables in an associative array using two different methods: dot
syntax (
someObj.myColor) and array syntax (someObj[‘myColor’]).
NOTE
The previous example used an Object constructor to create an associative array.

Table of Contents

Related product manuals