Skip to main content
Known Participant
August 7, 2009
Question

Actionscript 3.0 looping through stage and DisplayObject items

  • August 7, 2009
  • 2 replies
  • 2793 views

Actionscript 3.0 looping through stage and DisplayObject items

Hello,
   I'm trying to convert to Actionscript 3.0 and I'm trying to find out how to loop through Stage objects as well as DisplayObject.

With AS 2.0 - I would only have to do:


for(var i in _root) // or MovieClip, this, ect
{
    trace("i: " + i + " : " + _root.);
}

In AS 3.0 I found this equivalent which works w/ some objects:

public function debug_object(o:Object, show_all:Boolean):void
{
    import flash.utils.*;
    var def:XML       = describeType(o);
    var props:XMLList    = def..variable.@name;
    if(show_all)       props += def..accessor.@name;

    for each (var prop:String in props)
    {
        trace(prop + ": " + o[prop]);
    }
}

this.showChildren(Stage);

This kind of works but the o[prop] always traces undefined and it's not showing any movieClips on the stage.

I would like to be able to have the debug object function be able to take Stage and DisplayObject arguments like such:


this.debug_object(Stage, true);
this.debug_object(flash.display.DisplayObject, true);

Any ideas on how I could achieve this?

Thanks,
Clem C

This topic has been closed for replies.

2 replies

Known Participant
August 7, 2009

MovieClip is the superclass of videoControls.

August 7, 2009

Good. I thought perhaps you were using something other than a DisplayObject.

So you'll need to check that the stage property if not null before using it.

Known Participant
August 7, 2009

When I trace it  in the class - it's definitely null.      Is that because the class isn't  fully loaded at that time?

In as 2, I would do this in the constructor:

public function myClass()

{

    this.onLoad = init;

}

private function init():Void

{

}

August 7, 2009

You're processing Class objects, not instances:

this.debug_object(Stage, true);
this.debug_object(flash.display.DisplayObject, true);

Try sending the stage instance:

this.debug_object(this.stage, true);

Note: You'll need to handle errors, such as this:

Error: Error #2071: The Stage class does not implement this property or method.
at Error$/throwError()
at flash.display::Stage/get textSnapshot()
at Untitled_fla::MainTimeline/debug_object()
at Untitled_fla::MainTimeline/frame1()

Known Participant
August 7, 2009

Raymond,

    Big thanks for your help and great advice.    The only problem is that I'm trying to access the Stage from a class method and I get this error:

1119: Access of possibly undefined property stage through a reference with static type com:videoControl.

August 7, 2009

You'll need to wait until your com.VideoControl instance is added to the stage before you can run your procedure.

In your ctor, add something like this:

this.addEventListener(Event.ADDED_TO_STAGE, someHandler);

... and add a handler:

function someHandler(event:Event)

{

     if(event.type == Event.ADDED_TO_STAGE)

     {

          // do stuff here

     }

}