13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Working with arrays<br />

Note that the key named aspect ratio contains a space character. This is possible with the array access ([]) operator,<br />

but g<strong>en</strong>erates an error if attempted with the dot operator. Using spaces in your key names is not recomm<strong>en</strong>ded.<br />

The second way to create an associative array is to use the Array constructor (or the constructor of any dynamic class)<br />

and th<strong>en</strong> use either the array access ([]) operator or the dot operator (.) to add key and value pairs to the array. If you<br />

declare your associative array to be of type Array, you cannot use an object literal to initialize the array. The following<br />

example creates an associative array named monitorInfo using the Array constructor and adds a key called type and<br />

a key called resolution, along with their values:<br />

var monitorInfo:Array = new Array();<br />

monitorInfo["type"] = "Flat Panel";<br />

monitorInfo["resolution"] = "1600 x 1200";<br />

trace(monitorInfo["type"], monitorInfo["resolution"]);<br />

// output: Flat Panel 1600 x 1200<br />

There is no advantage in using the Array constructor to create an associative array. You cannot use the Array.l<strong>en</strong>gth<br />

property or any of the methods of the Array class with associative arrays, ev<strong>en</strong> if you use the Array constructor or the<br />

Array data type. The use of the Array constructor is best left for the creation of indexed arrays.<br />

Associative arrays with object keys (Dictionaries)<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

You can use the Dictionary class to create an associative array that uses objects for keys rather than strings. Such arrays<br />

are sometimes called dictionaries, hashes, or maps. For example, consider an application that determines the location<br />

of a Sprite object based on its association with a specific container. You can use a Dictionary object to map each Sprite<br />

object to a container.<br />

The following code creates three instances of the Sprite class that serve as keys for the Dictionary object. Each key is<br />

assigned a value of either GroupA or GroupB. The values can be of any data type, but in this example both GroupA and<br />

GroupB are instances of the Object class. Subsequ<strong>en</strong>tly, you can access the value associated with each key with the array<br />

access ([]) operator, as shown in the following code:<br />

Last updated 6/6/2012<br />

38

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!