Arrays

Arrays

Flash CS4 ActionScript 3.0 Tutorial

 

How to Organize Several Pieces of Data Using an Array in Flash

 

 

Arrays are used to organize large amounts of data. Arrays can be described as multi-dimensional variables that can hold several values. For instance you may have a list of names, phone numbers and email addresses.

You could set up three Array variables and have the first slot (slot [0]) of the memberName variable be John. The first slot of the phNumber variable would be John's phone number. John's email could be located in the first position of the eMail variable.

With this organization it would be easy to retrieve John's name, phone number and email address.

For more information on Arrays, see topics, Accessing Array Data and Loading an Array.

 

Step One

Open a new Flash document and name the default layer Actions.

Step Two

Option double-click (PC - Alt double-click) frame one of the Actions layer.

Step Three

Copy/Paste the following code to the ActionScript panel.

var memberName:Array = new Array("John", "Gail");

var phNumber:Array = new Array("555-1212", "555-1313");

var eMail:Array = new Array("john@pixelmail.com", "gail@pixelmail.com");

trace(memberName[0] + " " + phNumber[0] + " " + eMail[0]);

trace(memberName[1] + " " + phNumber[1] + " " + eMail[1]);

Step Four

Publish your movie by pressing Cmd/Return (PC - Ctrl/Enter). Each member's name, phone number and email address shows up in the Output panel.

Dissecting the Code

The first three lines of the code at Step Three establish three new variables, memberName, phNumber and eMail. For the memberName variable, John is loaded in slot [0] and Gail is in slot [1]. The values for the other two variables are loaded using the same pattern.

To see the contents of the Arrays, we added two trace statements. The quotation marks with a space between them provides spacing in the Output window.

 

Contribute Ad