Game Development Reference
In-Depth Information
When to use: The best time to use an Array is when you need
your items to be able to be sorted and their order matters.
Arrays also do not have to store all of the same type of item,
making them a little bit more flexible for general-purpose use
(see section
Vectors,
below)
Vectors
A Vector is simply a typed Array, meaning that all items in the list
must be of the same type. By enforcing typing, Vectors are faster to
iterate through and process and take up less memory. They also
have the option to be of a fixed length, that is, no more items can
be added to them. They are slightly differently than Arrays, but all
their other methods are the same.
var enemyList:Vector.
<
Sprite
>
= new Vector.
<
Sprite
>
();
for (var i:int = 0; i
10; i++) {
var enemy:Sprite = new Sprite();
enemyList.push(enemy);
<
}
Pros: All of the pros of Arrays except ability to combine
differently typed Vectors, faster to iterate through than Arrays
Cons: Still requires iteration to access specific items, so a little
slower than objects, requires Flash Player 10 (not available if
you
re still publishing for Player 9)
When to use: If at all possible, you should always use Vectors
over Arrays if all of your items are of the same type. Typically,
in a game, your lists will already be homogeneous anyway, so
switching to a Vector give you some extra performance
'
Dictionaries
Just as the Vector object improved on Arrays for ordered storage,
AS3 added a new class to improve on basic Objects for storing
unordered lists: the Dictionary object. Unlike regular Objects,
whichrequireastringtobeusedasthekeyforanitem,Diction-
aries can use any data type, including the item itself. This makes
them even easier to use for complex data types because you don
t
havetohaveauniquestringtoidentifyitems.TheDictionary
constructor also contains one parameter called weakKeys ,which
defaults to false. When a Dictionary uses weakKeys if an item in
thelistanditskeyareoneinthesame,andyouremovetheitem
from the list, the key is removed as well. For this reason, I like
to set weakKeys to true. Here is the enemyList example, using a
Dictionary object.
'
var enemyList:Dictionary = new Dictionary(true);
for (var i:int = 0; i < 10; i++) {
Search Nedrilad ::




Custom Search