Traversing the display list

February 2, 2010 on 10:32 pm | In Actionscript, Games | 3 Comments

A great HaXe feature is that you can define your own Iterator and execute it with the for-syntax.
It can be used in many different ways and drastically improves readability of your code. AS3 developers often need to look at the display list, so I wrote a basic DisplayListIterator to handle this task. Here is an example:

using de.polygonal.gl.DisplayListIterator;
...
for (i in Lib.current.stage) trace(i);

This will print out all display objects in the display list.

If you are not familiar with HaXe, Lib.current.stage points to the MovieClip of the Document class, and the using statement automatically creates a DisplayListIterator whenever it is called on a DisplayObjectContainer. So the statement ‘Class.method(arg)’ is transformed to ‘arg.method()’. Without ‘using’ I would have to write:

import de.polygonal.gl.DisplayListIterator;
...
for (i in new DisplayListIterator(Lib.current.stage)) trace(i);

Let’s finish with a simple example that changes the text color of all text fields to red inside a DisplayObjectContainer.

var container:Sprite = myTextFieldContainer;
for (i in container)
{
  if (Std.is(i, TextField))
  {
    cast(i, TextField).textColor = 0xff0000;
  }
}

Useful, isn’t it :)

3 Comments »

RSS feed for comments on this post. TrackBack URI

  1. Very cool, and usefull to (for-in love).

    But does the DisplayListIterator work with null-references in the display list? Sometimes when checking complex timeline animations there will be null-entries in the displaylist (seem to concide with layers with empty key-frames).

    Comment by Bart — February, 3 2010 #

  2. It should have no problems with null-reference. The iterator is non-recursive and only pushes values onto the stack that aren’t null.

    Comment by Michael — February, 3 2010 #

  3. Yes, very useful ;) Use it on one of my projects, 10x.

    Comment by Thomas — May, 11 2010 #

Leave a comment

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Proudly powered by WordPress Theme based upon Pool theme by Borja Fernandez.
Entries and comments feeds.