Game Development Reference
In-Depth Information
var enemy:Sprite = new Sprite();
enemyList[enemy] = enemy;
}
As you can probably already tell, getting access to a specific
item in a Dictionary is also easier than with a traditional object.
With Dictionary objects, it is necessary to use the new for each
loop in AS3.
for each (var enemy:Sprite in enemyList) {
//DO SOMETHING TO ENEMY SPRITE
}
The delete command applies here the same way it does with
regular objects.
protected function enemyClicked(e:MouseEvent) {
delete enemyList[e.target];
}
Pros: Ability to key off any value, including items themselves;
fast, direct access of an object to individual items; can store
items of any type together
Cons: Unordered, not as helpful for lists of primitive values like
strings or numbers
When to use: As much as possible! Outstanding for storing all
unordered lists of complex objects
ByteArrays
Although not useful for storing lists of objects, the ByteArray class
is designed to store raw binary data, making it a perfect (in fact,
the only) candidate container for things such as image or sound
data. We won
t really use ByteArrays in this topic, but they are very
fast and worth mentioning since they are often overlooked.
'
So What Should I Use For My Lists?
That answer, as with so many questions, is,
Itendto
like to use Dictionaries to keep track of all my object lists in a
game and then use Arrays or Vectors only when I need their sort-
ingabilities.Youreallycan
Depends.
'
tbeataDictionaryforeaseofuseor
speed. If I must have an ordered list, I would prefer a Vector to an
Arrayduetoitsslightedgeinspeed.Thisisnottosaythatbasic
Objects and Arrays are no longer useful. Objects are still great con-
tainersfordynamicdatabutnotasfastforlistsasDictionaries.
Arrays are great to fall back on if you can
t guarantee that all your
list items will be of the same type or if you
'
'
re working with older
classes that aren
'
t configured to handle Vectors.
Search Nedrilad ::




Custom Search