Array Quick Guide Tutorial
[ June 29, 2003 ] by Marco Lapi, a.k.a Lapo
This article is designed to be a quick reference on Arrays for all actionscript developers.
Arrays are very important in defining data structures and they are heavily used in all types of games. (Note: some basic actionscript and objects knowledge is required)


To put it simple an array is a representation of a data structure.
Whatever data comes to your mind it can defined by an array : from a generic sequence of numbers to a contact list or a collection of records etc...

An example could be the list of days in the week, something like:


An array can be made up of primitive type values like strings, numeric values, booleans or complex type values like other arrays or objects.



In an object oriented environment like actionscript you should expect arrays to be objects themselves and this is exactly what is going on. If you have a little experience in ObjectOriented programming you should know that every object has to be constructed.
In actionscript we use the new keyword to create a new Array, look at this sample:

myArray = new Array()
otherArray = new Array("June","July","August","September")

There are 3 ways of constructing an array:
the first one will create an empty object while the second one will populate it with the passed parameter .

The third way looks like this:

myArray = new Array(16)

in this case we create an empty array that can contain 16 elements.
So passing one single numeric value to the constructor will create an array with n number of empty elements.
If you pass one or more numeric (or non-numeric) values these will populate the array with those values.



All the elements in an array are indexed with a numeric value starting from 0.
If you go back to the example of the months :

otherArray = new Array("June","July","August","September")

the array is represented like this:

0: June
1: July
2: August
3: September

In other words we can retrieve each element like this:

trace( otherArray[0] ) // will display >> June
trace( otherArray[3] ) // will display >> September

the length property of the Array object tells us how many items are in one array

trace( otherArray.length ) // will display 4

Once we know this, it should be very ease to cycle trough all the array elements:

for (var i=0; i < otherArray.length; i++)
	trace( otherArray[i] )

(continues on page 2)

Name: Marco Lapi, a.k.a Lapo
Location: Fossano, Italy
Age: 34
Flash experience: started out with Flash 4 back in 1999
Job: web designer/developer
| Homepage | News | Games | Articles | Multiplayer Central | Reviews | Spotlight | Forums | Info | Links | Contact us | Advertise | Credits |

| | | |

gotoAndPlay() v 3.0.0 -- (c)2003-2008 gotoAndPlay() Team -- P.IVA 03121770048