views:

566

answers:

2

This is a document class for a display object walker. Make sure to turn off the strict mode (howto here) when testing the class. Also put some stuff on the stage. When the strict mode is turned off the object walker works just fine. However, I want to make it work in strict mode too. I have tried changing the problematic parts, and addig (dispObj as DisplayObject), with no luck.

package {

    import flash.display.MovieClip;
    import flash.display.DisplayObject;
    import flash.display.DisplayObjectContainer;

    public class DisplayWalker extends MovieClip {

     public function DisplayWalker() {
      showChildren(stage, 0);
     }



     private function padIndent(indents:int):String {
      var indent:String = "";
      for (var i:uint = 0; i < indents; i++) {
       indent += "        ";
      }
      return indent;
     }

     private function showChildren(dispObj:DisplayObject, indentLevel:Number):void {
      for (var i:uint = 0; i < dispObj.numChildren; i++) {
       var obj:DisplayObject = dispObj.getChildAt(i);
       if (obj is DisplayObjectContainer) {
        trace(padIndent(indentLevel), obj, obj.name);
        showChildren(obj, indentLevel + 1);
       } else {
        trace(padIndent(indentLevel), obj);
       }
      }
     }
    }
}
+1  A: 

Your class will generate compile time errors in Strict mode because you're trying to access the numChildren and getChildAt methods, which aren't available on the DisplayObject class, but first on one of it's subclasses, DisplayObjectContainer.

The reason it is working in non-Strict mode is that, at runtime, you're effectively passing in subclasses of DisplayObjectContainer (Stage, Sprite, etc).

Just replace DisplayObject with DisplayObjectContainer as the type for dispObj in your showChildren method. DisplayObjects cannot have children and are always leafs in the display object tree, something your showChildren method will have to account for.

Stiggler
"DisplayObjects cannot have children and are always leafs in the display object tree, something your showChildren method will have to account for." There is an if for that. if (obj is DisplayObjectContainer) {...} else {...}
Richard J. Terrell
Thanks! Changing the the parameter to dispObj:DisplayObjectContainer and the recursive call to ShowChildren(obj as DisplayObjectContainer) solved to problem.
Richard J. Terrell
A: 

Stiggler is on the right track, but properly didn't see that you already check for DisplayObjectContainers.

You just need to modify your code slightly. I didn't test the code, but in any case you should be able to figure it out ;)

private function showChildren(dispObj:DisplayObject, indentLevel:Number):void
{
    var dOC:DisplayObjectContainer = dispObj as DisplayObjectContainer;

    if(dOC == null)
    {
        trace(padIndent(indentLevel),obj);
    }
    else
    {    
        trace(padIndent(indentLevel), obj, obj.name);

        var obj:DisplayObject = null;

        for (var i:uint = 0; i < dispObj.numChildren; i++)
        {
            obj = dOC.getChildAt(i);
            showChildren(obj, indentLevel + 1);
        }
    }
}
Lillemanden
Actually what Stiggler say is OK. The function gets called with stage at start and at every further recursion a displayobjectcontainer gets passed. This is the reason getChildAt never fails.
Richard J. Terrell