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

songTitles[5] = "Happy Birthday";<br />

If the Array or Vector doesn’t already have an elem<strong>en</strong>t at that index, the index is created and the value is stored there.<br />

If a value exists at that index, the new value replaces the existing one.<br />

An Array object allows you to create an elem<strong>en</strong>t at any index. However, with a Vector object you can only assign a<br />

value to an existing index or to the next available index. The next available index corresponds to the Vector object’s<br />

l<strong>en</strong>gth property. The safest way to add a new elem<strong>en</strong>t to a Vector object is to use code like this listing:<br />

myVector[myVector.l<strong>en</strong>gth] = valueToAdd;<br />

Three of the Array and Vector class methods—push(), unshift(), and splice()—allow you to insert elem<strong>en</strong>ts into<br />

an indexed array. The push() method app<strong>en</strong>ds one or more elem<strong>en</strong>ts to the <strong>en</strong>d of an array. In other words, the last<br />

elem<strong>en</strong>t inserted into the array using the push() method will have the highest index number. The unshift() method<br />

inserts one or more elem<strong>en</strong>ts at the beginning of an array, which is always at index number 0. The splice() method<br />

will insert any number of items at a specified index in the array.<br />

The following example demonstrates all three methods. An array named planets is created to store the names of the<br />

planets in order of proximity to the Sun. First, the push() method is called to add the initial item, Mars. Second, the<br />

unshift() method is called to insert the item that belongs at the front of the array, Mercury. Finally, the splice()<br />

method is called to insert the items V<strong>en</strong>us and Earth after Mercury, but before Mars. The first argum<strong>en</strong>t s<strong>en</strong>t to<br />

splice(), the integer 1, directs the insertion to begin at index 1. The second argum<strong>en</strong>t s<strong>en</strong>t to splice(), the integer<br />

0, indicates that no items should be deleted. Finally, the third and fourth argum<strong>en</strong>ts s<strong>en</strong>t to splice(), V<strong>en</strong>us and<br />

Earth, are the items to be inserted.<br />

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

planets.push("Mars"); // array cont<strong>en</strong>ts: Mars<br />

planets.unshift("Mercury"); // array cont<strong>en</strong>ts: Mercury,Mars<br />

planets.splice(1, 0, "V<strong>en</strong>us", "Earth");<br />

trace(planets); // array cont<strong>en</strong>ts: Mercury,V<strong>en</strong>us,Earth,Mars<br />

The push() and unshift() methods both return an unsigned integer that repres<strong>en</strong>ts the l<strong>en</strong>gth of the modified array.<br />

The splice() method returns an empty array wh<strong>en</strong> used to insert elem<strong>en</strong>ts, which may seem strange, but makes more<br />

s<strong>en</strong>se in light of the splice() method’s versatility. You can use the splice() method not only to insert elem<strong>en</strong>ts into<br />

an array, but also to remove elem<strong>en</strong>ts from an array. Wh<strong>en</strong> used to remove elem<strong>en</strong>ts, the splice() method returns<br />

an array containing the elem<strong>en</strong>ts removed.<br />

Note: If a Vector object’s fixed property is true, the total number of elem<strong>en</strong>ts in the Vector can’t change. If you try to<br />

add a new elem<strong>en</strong>t to a fixed-l<strong>en</strong>gth Vector using the techniques described here, an error occurs.<br />

Retrieving values and removing array elem<strong>en</strong>ts<br />

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

The simplest way to retrieve the value of an elem<strong>en</strong>t from an indexed array is to use the array access ([]) operator. To<br />

retrieve the value of an indexed array elem<strong>en</strong>t, use the Array or Vector object name and index number on the right<br />

side of an assignm<strong>en</strong>t statem<strong>en</strong>t:<br />

var myFavoriteSong:String = songTitles[3];<br />

It’s possible to attempt to retrieve a value from an Array or Vector using an index where no elem<strong>en</strong>t exists. In that case,<br />

an Array object returns the value undefined and a Vector throws a RangeError exception.<br />

Last updated 6/6/2012<br />

31

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

Saved successfully!

Ooh no, something went wrong!