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

Multidim<strong>en</strong>sional arrays<br />

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

Multidim<strong>en</strong>sional arrays contain other arrays as elem<strong>en</strong>ts. For example, consider a list of tasks that is stored as an<br />

indexed array of strings:<br />

var tasks:Array = ["wash dishes", "take out trash"];<br />

If you want to store a separate list of tasks for each day of the week, you can create a multidim<strong>en</strong>sional array with one<br />

elem<strong>en</strong>t for each day of the week. Each elem<strong>en</strong>t contains an indexed array, similar to the tasks array, that stores the<br />

list of tasks. You can use any combination of indexed or associative arrays in multidim<strong>en</strong>sional arrays. The examples<br />

in the following sections use either two indexed arrays or an associative array of indexed arrays. You might want to try<br />

the other combinations as exercises.<br />

Two indexed arrays<br />

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

Wh<strong>en</strong> you use two indexed arrays, you can visualize the result as a table or spreadsheet. The elem<strong>en</strong>ts of the first array<br />

repres<strong>en</strong>t the rows of the table, while the elem<strong>en</strong>ts of the second array repres<strong>en</strong>t the columns.<br />

For example, the following multidim<strong>en</strong>sional array uses two indexed arrays to track task lists for each day of the week.<br />

The first array, masterTaskList, is created using the Array class constructor. Each elem<strong>en</strong>t of the array repres<strong>en</strong>ts a<br />

day of the week, with index 0 repres<strong>en</strong>ting Monday, and index 6 repres<strong>en</strong>ting Sunday. These elem<strong>en</strong>ts can be thought<br />

of as the rows in the table. You can create each day’s task list by assigning an array literal to each of the sev<strong>en</strong> elem<strong>en</strong>ts<br />

that you create in the masterTaskList array. The array literals repres<strong>en</strong>t the columns in the table.<br />

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

masterTaskList[0] = ["wash dishes", "take out trash"];<br />

masterTaskList[1] = ["wash dishes", "pay bills"];<br />

masterTaskList[2] = ["wash dishes", "d<strong>en</strong>tist", "wash dog"];<br />

masterTaskList[3] = ["wash dishes"];<br />

masterTaskList[4] = ["wash dishes", "clean house"];<br />

masterTaskList[5] = ["wash dishes", "wash car", "pay r<strong>en</strong>t"];<br />

masterTaskList[6] = ["mow lawn", "fix chair"];<br />

You can access individual items on any of the task lists using the array access ([]) operator. The first set of brackets<br />

repres<strong>en</strong>ts the day of the week, and the second set of brackets repres<strong>en</strong>ts the task list for that day. For example, to<br />

retrieve the second task from Wednesday’s list, first use index 2 for Wednesday, and th<strong>en</strong> use index 1 for the second<br />

task in the list.<br />

trace(masterTaskList[2][1]); // output: d<strong>en</strong>tist<br />

To retrieve the first task from Sunday’s list, use index 6 for Sunday and index 0 for the first task on the list.<br />

trace(masterTaskList[6][0]); // output: mow lawn<br />

Last updated 6/6/2012<br />

41

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

Saved successfully!

Ooh no, something went wrong!