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 />

To access an individual elem<strong>en</strong>t of an indexed array, you use the array access ([]) operator to specify the index position<br />

of the elem<strong>en</strong>t you wish to access. For example, the following code repres<strong>en</strong>ts the first elem<strong>en</strong>t (the elem<strong>en</strong>t at index<br />

0) in an indexed array named songTitles:<br />

songTitles[0]<br />

The combination of the array variable name followed by the index in square brackets functions as a single id<strong>en</strong>tifier.<br />

(In other words, it can be used in any way a variable name can). You can assign a value to an indexed array elem<strong>en</strong>t by<br />

using the name and index on the left side of an assignm<strong>en</strong>t statem<strong>en</strong>t:<br />

songTitles[1] = "Symphony No. 5 in D minor";<br />

Likewise, you can retrieve the value of an indexed array elem<strong>en</strong>t by using the name and index on the right side of an<br />

assignm<strong>en</strong>t statem<strong>en</strong>t:<br />

var nextSong:String = songTitles[2];<br />

You can also use a variable in the square brackets rather than providing an explicit value. (The variable must contain<br />

a non-negative integer value such as a uint, a positive int, or a positive integer Number instance). This technique is<br />

commonly used to “loop over” the elem<strong>en</strong>ts in an indexed array and perform an operation on some or all the elem<strong>en</strong>ts.<br />

The following code listing demonstrates this technique. The code uses a loop to access each value in an Array object<br />

named oddNumbers. It uses the trace() statem<strong>en</strong>t to print each value in the form “oddNumber[index] = value”:<br />

var oddNumbers:Array = [1, 3, 5, 7, 9, 11];<br />

var l<strong>en</strong>:uint = oddNumbers.l<strong>en</strong>gth;<br />

for (var i:uint = 0; i < l<strong>en</strong>; i++)<br />

{<br />

trace("oddNumbers[" + i.toString() + "] = " + oddNumbers[i].toString());<br />

}<br />

The Array class<br />

The first type of indexed array is the Array class. An Array instance can hold a value of any data type. The same Array<br />

object can hold objects that are of differ<strong>en</strong>t data types. For example, a single Array instance can have a String value in<br />

index 0, a Number instance in index 1, and an XML object in index 2.<br />

The Vector class<br />

Another type of indexed array that’s available in ActionScript 3.0 is the Vector class. A Vector instance is a typed array,<br />

which means that all the elem<strong>en</strong>ts in a Vector instance always have the same data type.<br />

Note: The Vector class is available starting with Flash Player 10 and Adobe AIR 1.5.<br />

Wh<strong>en</strong> you declare a Vector variable or instantiate a Vector object, you explicitly specify the data type of the objects<br />

that the Vector can contain. The specified data type is known as the Vector’s base type. At run time and at compile time<br />

(in strict mode), any code that sets the value of a Vector elem<strong>en</strong>t or retrieves a value from a Vector is checked. If the<br />

data type of the object being added or retrieved doesn’t match the Vector’s base type, an error occurs.<br />

In addition to the data type restriction, the Vector class has other restrictions that distinguish it from the Array class:<br />

A Vector is a d<strong>en</strong>se array. An Array object may have values in indices 0 and 7 ev<strong>en</strong> if it has no values in positions 1<br />

through 6. However, a Vector must have a value (or null) in each index.<br />

A Vector can optionally be fixed-l<strong>en</strong>gth. This means that the number of elem<strong>en</strong>ts the Vector contains can’t change.<br />

Access to a Vector’s elem<strong>en</strong>ts is bounds-checked. You can never read a value from an index greater than the final<br />

elem<strong>en</strong>t (l<strong>en</strong>gth - 1). You can never set a value with an index more than one beyond the curr<strong>en</strong>t final index. (In<br />

other words, you can only set a value at an existing index or at index [l<strong>en</strong>gth].)<br />

Last updated 6/6/2012<br />

27

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

Saved successfully!

Ooh no, something went wrong!