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)

(continues from page 1)


As we said before arrays not only can store primitive values but they can contain other objects and arrays.

Look at this example:

myData = new Array()

//--- Create a new Object --------------------
myObject = new Object()

myObject.myName = "Marco"
myObject.myLocation = "Italy"
myObject.myAge = 30

myData[0] = myObject
myData[1] = { x: 200, y: 400 }
myData[2] = 100.99
myData[3] = new Array("flash","director","dreamweaver")
myData[4] = "Last item of this complex array"

There's a lot of stuff going on in this example:

in the first line a new, empty array is created. Then a new object is created with three properties.

Moving on the next lines you will notice that the first five items of the array are filled:

item at 0: is the object we previously constructed
item at 1: use a literal expression to create a new Object with 2 properties, x and y
item at 2: a float value
item at 3: contains a new Array containing 3 strings
item at 4: a string

At this point you can retrieve the array elements like this:

trace(myData[0].myLocation) 	// will output >> italy
trace(myData[1].y)			// will output >> 4
trace(myData[2])			// will output >> 100.99
trace(myData[3][1])			// will output >> director
trace(myData[4])			// will output >> Last item of this complex array	

You can download the source code of this example.

So far you learned that arrays in actionscript can contain different elements like primitive types and complex types as well like other
arryas and objects. Now I want to show you a more game-oriented code snippet that could be used to create an enemy data structure.

In our DickDynamite we have a variable number of enemies in every level and each of them has his own properties: position, speed, status (dead or alive) etc...

To store this information we could create an empty object with the properties that we need:

// Create the objects for each enemy
enemy0 = new Object();
enemy1 = new Object();
enemy2 = new Object();
// Setup enemy properties enemy0.posx = 10; enemy0.posy = 10; enemy0.speed = 12; = true; enemy1.posx = 1; enemy1.posy = 5; enemy1.speed = 7; = false; enemy2.posx = 6; enemy2.posy = 7; enemy2.speed = 18; = true; // Add objects to an empty array enemies = new Array() enemies[0] = enemy0; enemies[1] = enemy1; enemies[2] = enemy2; // Cycle through the array for (i=0; i < enemies.length; i++) { if (enemies[i].active) // do something with it }

Here we setup 3 objects with their properties and then we populate an "enemies" array so that it makes it easier to cycle through all enemies in the game. We have created our first basic datastructure for a game.

(continues on page 3)

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