AS3 Helper Class – ShowDisplayList()

We are building an AS3 Helper class to speed up development on Actionscript 3 projects. Feel free to use these methods in your own Helper class, or to share your own methods that you find useful.

Wrap()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 
public class Helper
{
import flash.display.*;
/**
 * This method accepts a
 * DisplayObjectContainer instance
 * and lists the children in its
 * display list by depth, name and
 * type.
 */		
public static function ShowDisplayList
	(theDisplayObjectContainer:DisplayObjectContainer):void
{
	var len:uint = theDisplayObjectContainer.numChildren;
	for(var i:uint = 0; i<len; i++)
	{
		var theChildObject:DisplayObject =
			theDisplayObjectContainer.getChildAt(i); 
		trace("level " + i + ":" + theChildObject.name +
			" " + theChildObject);
	}
	/* output example:
		level0:spaceship [object MovieClip]
		level1:asteroid0 [object MovieClip]
		level2:asteroid1 [object MovieClip]
	*/
}
}

Usage

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var theParentClip:MovieClip = new MovieClip();
	// create a clip that will hold other clips
 
for(var i:uint = 0; i<20; i++)
{
	// create 20 MovieClips and add them to
	// theParentClip's display list
	var theChildClip:MovieClip = new MovieClip();
	theChildClip.name = "clip" + i;
	theParentClip.addChild(theChildClip);
}
 
Helper.ShowDisplayList(theParentClip);
	// display a report on what's in
	// theParentClip's display list

Comments

Whenever i teach an intro Flash/Actionscript lesson to someone, and i want to stress the importance of the trace(); command, i say that there’s usually a difference between what you think Flash is doing, and what Flash thinks Flash is doing. trace(); is a fast, easy tool to help you find a discrepency between your understanding of what’s going on, and what’s actually happening!

ShowDisplayList() helps you quickly and easily visualize what’s in your DisplayObjectContainer object and at which depth levels. It’s very handy, because looks can be deceiving – when you look at graphics on the screen, you can’t always tell when there are two or more instances of the same MovieClip sitting on top of each other, or if the clip that didn’t show up is actually sitting behind another clip, etc etc. This method sorts that all out for you, and has already saved my bacon on a few projects.

For more AS3 Helper Classses and a pile of other useful stuff, check out our Flash and Actionscript 911 feature.

2 thoughts on “AS3 Helper Class – ShowDisplayList()

  1. Brent

    Personally, I enjoy these three: Normalize, Interpolate, Map. The format comes from Processing, but Keith Peters ported them to AS. Map can be used to recalculate values so they fit in the corresponding proportion on a different size container. (Did I make that sound confusing enough?)

    Its better explained here:
    http://www.bit-101.com/blog/?p=1242

    Reply
  2. Ryan

    The first i heard of Processing was yesterday, when i thumbed through a book on it at the Rich Media Institute. i was pulmb confused. To a n00b, it looked like a general language-agnostic guide to graphics processing … but then it was showing all these specific code examples. i started getting wise when you posted this comment, and now that i’ve looked it up on Wikipedia, i finally understand that “Processing” is an actual language.

    But MAN … that’s like calling your new salty snack food “Food”.

    Looking at your 3 methods – i first heard of “lerp” (linear interpolation) while learning C#. There are a bunch of C#/XNA methods that i’m actually stealing and rewriting in Actionscript because they’re such great ideas! Helper.Wrap is actually one of them:

    http://www.untoldentertainment.com/blog/2008/11/09/as3-helper-class-wrap/

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *