FireworksColdFusionDreamweaverFreehandFlashMXHome
Latest New Content

Latest Free Content
View All
Free Content
Accessibility
CMX Learning Guides
Hosted by enterhost

Using Arrays in ActionScript - Part 1

By: Paul Newman

Page 1 of 8

Set for printing

Next

Arrays are a staple of ActionScript, and for that matter, ECMAScript, upon which ActionScript is based. In this tutorial, we'll examine how to create different types of arrays, and how to loop through them using ActionScript.

NOTE: According to the Flash documentation, the current version of ActionScript is based on the ECMA-262 Edition 4 proposal.

Chances are you're already familiar with using variables in ActionScript to store data. If a variable holds a reference to a single piece of data, then we can think of an array as a variable that stores a grouping of related data. One analogy is that an array is like an audio CD. An audio CD contains a bunch of related data — its songs — and each song has a track number. As an example, we'll use the Beatles album Help!, which contains 14 tracks:

1. Help!
2. The Night Before
3. You've Got to Hide Your Love Away
4. I Need You
5. Another Girl
6. You're Going to Lose That Girl
7. Ticket to Ride
8. Act Naturally
9. It's Only Love
10. You Like Me Too Much
11. Tell Me What You See
12. I've Just Seen A Face
13. Yesterday
14. Dizzy Miss Lizzie

If you want to play "Ticket to Ride" on your stereo, you simply choose Track 7. If you press Shuffle on your stereo, the tracks are resorted in random order, but "Ticket to Ride" is still Track 7:

11. Tell Me What You See
3. You've Got to Hide Your Love Away
10. You Like Me Too Much
4. I Need You
7. Ticket to Ride
12. I've Just Seen A Face
9. It's Only Love
2. The Night Before
5. Another Girl
13. Yesterday
6. You're Going to Lose That Girl
1. Help!
14. Dizzy Miss Lizzie
8. Act Naturally

In this sense, each track number represents an index for the audio CD, not unlike primary key columns in a database. Without track numbers, there would be no way for your stereo to identify which song to play. Arrays also have numeric indexes, which are used to identify each element of an array. Unlike CD track numbers, however, ActionScript arrays start counting from 0, rather than 1. If we were going to represent Help! as an ActionScript array, here is one way we might do it:

var help = new Array();
help[0] = "Help!";
help[1] = "The Night Before";
help[2] = "You've Got to Hide Your Love Away";
help[3] = "I Need You";
help[4] = "Another Girl";
help[5] = "You're Going to Lose That Girl";
help[6] = "Ticket to Ride";
help[7] = "Act Naturally";
help[8] = "It's Only Love";
help[9] = "You Like Me Too Much";
help[10] = "Tell Me What You See";
help[11] = "I've Just Seen A Face";
help[12] = "Yesterday";
help[13] = "Dizzy Miss Lizzie";

Listing 1  Defining and populating the help array object in ActionScript

On the first line, we define an array object called help using the new Array() constructor, and on the subsequent lines, we populate the array with elements using the array access operator ([]).

Page 1 of 8 1 2 3 4 5 6 7 8 Next


download
Download Support Files


Keywords
flash mx 2004, actionscript, arrays, object literal, array literal, associative array