Game Development Reference
In-Depth Information
Pros: Easy to access items, fast to iterate through, easy to
garbage collect
Cons: Unordered, must have a unique string property such as a
name associated with whatever you
restoring(usingthesame
string twice will override the first one)
When to use: Best used when you
'
re not interested in the
order of a group of items and when you have a unique string
identifier like a name to use
'
Arrays
Up until AS3, Objects and Arrays were the only two native types of
data storage available in Flash. An Array is an ordered list of items
that are indexed by number, starting from 0. Arrays can have an
unlimited number of items added to them using the push , unshift ,
and splice commands. When an item is removed using the pop ,
shift ,or splice commands, the array size, or length , is reduced.
Items in the list can be set to null values, but the null still occupies
a slot in the array. Like Objects, Arrays are easy to set up and use.
Once an Array is created, the push command is used to add items
to the end of it. Likewise, the unshift command can be used to
insert items at the front.
var enemyList:Array = new Array();
for (var i:int = 0; i < 10; i++) {
var enemy:Sprite = new Sprite();
enemyList.push(enemy);
}
One big advantage of Arrays is the ability to easily combine
them. Say you had separate lists of enemies, obstacles, and pick-
ups, and you needed to perform an operation on all of them and
also keep them in their discrete lists. You can use the concat
method to concatenate the Arrays together into one list and only
loop through one larger Array.
var combinedList:Array = enemyList.concat(obstacleList,
pickupList);
for (var i:int = 0; i < combinedList.length; i++) {
var item:Sprite = combinedList[i];
//PERFORM SOME OPERATION ON EACH ITEM
}
Another advantage of using Arrays is the availability of sorting
options. Because it is an ordered list, the order can be changed
dependent on almost criteria you specify using the sort and sortOn
commands. The sortOn method is particularly helpful when you
have an Array of DisplayObjects such Sprites. Say you wanted to
Search Nedrilad ::




Custom Search