views:

55

answers:

2

I'm working on a game that has many elements on stage, so I'm looking to optimize my loops. I know that Vector is faster than looping an array, but I'm also in some cases using:

while (i < numChildren)
    getChildAt(i)

...to update sprites.

My question is when I use getChildAt, is that accessing an Array or Vector or linked list or other? Should I instead store references to my objects in a Vector and loop through that instead?

+3  A: 

if you have a lots of elements on the stage, most of the time is probably spent rendering them.
also, it matters only little, how the child-list of DisplayObjectContainer is implemented, since the cost for function calls is orders of magnitude higher than access of Vectors or Arrays. I guess in fact the child-list is implemented using a C/C++ collection, instead of having all the overhead coming from ActionScript builtin collections.

So yes, storing all children in a Vector will allow faster lookup, although deletion will become expensive. Even insterting will become either more expensive (if you override all child manipulation methods to update the vector when making changes and listen to remove events) or more difficult.

you should run your game with the root sprite turned invisible, to see how much it consumes.
From my experience, this optimization will not yield any signifficant speedup. You should rather try to learn about existing optimization techniques for flash games.

greetz
back2dos

back2dos
+1  A: 

According to the docs, getChildAt() is a fast hash-table lookup (as compared to getChildByName() which is a (relatively) slower linked-list traversal.

davr