Traversing the display list

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:

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:

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 (, TextField))
    cast(i, TextField).textColor = 0xff0000;

Useful, isn’t it :)

3 thoughts on “Traversing the display list”

  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).

  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.

Comments are closed.