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 2)

ASSOCIATIVE ARRAYS

Until now we've been retrieving data from arrays using indexes, but there is also another way that can be very useful: using keys !
An associative array is an array that uses keys or names to retrive its items.
An example will explain this better:

// Associative array

myData = new Array()

myData["mazegame"] = "pacman"
myData["arcade"] = "bomberman"
myData["shooter"] = "galaxian"

trace(myData["mazegame"])	// displays >> pacman
trace(myData["arcade"])	// displays >> bomberman
trace(myData["shooter"])	// displays >> galaxian	

The code shows how you can associate a value to a certain name or key in an array. Simple and very useful !
You could also mix the the two approaches creating both indexes and keys to memoriza data into an array.
In this field actionscript really shows its power and flexibilty!

 

ARRAY METHODS

In actionscript an Array is treated as an object and it features a number of methods that are very useful.
It is not the purpose of this article to do an in-depth description of all of these methods, as these informations can be easily found
in the reference included with Flash MX.

We will just give a quick look at the most important of these methods:

myArray.push(item): appends a new item to the array, there's no need to specify a key or index.
myArray.pop() : removes the last element from the array, returning it's value.
( these two methods are commonly used together to simulate a stack )

myArray.unshift(it1,it2,... , itn) : adds one or more elements at the beginning of an array, shifting the other elements.
It returns the new lenght of the array
myArray.reverse(): reverses the order of the array. First item will be the last and viceversa.

There's also an external function that doesn't belong to the Array object called delete.
It is used to remove elements from an array like this:

delete myArray[4] // removes the element at that index
delete myArray["test"] // removes the element with that key

 

CONCLUSIONS

As you may have noticed, arrays are a very flexible tool to design data structures and their implementation in Actionscript is very powerful and more flexible than in other languages.
Arrays are used all over in the game developement field, and as you will start to experiment with these concepts you'll find many
ways of using them in your code.

For any questions about this article, drop us a line in the forums.


        
 
 
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
Website: http://www.gotoandplay.it/
 
 
| Homepage | News | Games | Articles | Multiplayer Central | Reviews | Spotlight | Forums | Info | Links | Contact us | Advertise | Credits |

| www.smartfoxserver.com | www.gotoandplay.biz | www.openspace-engine.com |

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