How to trace a full Display List with a recursive function?

℡╲_俬逩灬. 提交于 2019-12-10 11:44:42

问题


I want to see all instances on the Display List of a specific DisplayObjectContainer. How to do it with a recursive function to see beyond the first depth ?


回答1:


This is the simplest code that you can recursively see all the parents of a DisplayObject going to top from bottom. Add numChildren tweak and you can see all other children of the parent too.

function traceD(mc:DisplayObject) {
   trace(mc);
   if(mc.parent!=null) traceD(mc.parent);
}



回答2:


Here a simple recursive function to trace a Display List. In a MovieClip, this line : traceDisplayL(this, 2); can output:

 root1 MainMe
     Alone Symbol1
         Shape
     BigOne Symbol2
         Shape
         InBigOne Symbol3

The import section:

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

The traceDisplayL function: (don't use the currentDepth parameter, leave it as is)

    public function traceDisplayL(displayObject:DisplayObject, maxDepth:int = 20, levelSpace:String = " ", currentDepth:int = 0) {

        var className:String = String(getQualifiedClassName(displayObject));
        var indexOfString:int = className.lastIndexOf("::");
        className = (indexOfString < 0) ? className : className.slice(indexOfString + 2);

        var displayName:String =  String((displayObject.name.substr(0, 8) == "instance") ? "" : displayObject.name + " ");

        trace(levelSpace + displayName + className);   

        if (displayObject is DisplayObjectContainer && currentDepth < maxDepth) {       
            for (var i:int = 0; i < DisplayObjectContainer(displayObject).numChildren; i++) {
                traceDisplayL(DisplayObjectContainer(displayObject).getChildAt(i), maxDepth, levelSpace + "    ", currentDepth + 1);
            }
        }
    }

And a more elaborate one traceDisplayList (using two other functions). You will see text on the side if the object is a TextField, and if you want to:

  • only "instance" object (those who have a name);
  • data related to object (.x, .y, .width, .height);
  • parent object name, after the object name;

You can also limit the maximum depth, and/or skip all objects of a specific Class (if you don't want to see any Shape or Whatever in your trace).

    public static function traceDisplayList(displayObject:DisplayObject, instanceOnly:Boolean = false, withData:Boolean = false, withParent:Boolean = false, maxDepth:int = 100, skipClass:Class = null, levelSpace:String = " ", currentDepth:int = 0, parent:String = ""):void 
    {
        if (skipClass != null) { if (displayObject is skipClass) { return; } }

        var displayName:String = displayObjectCleanName(displayObject);
        displayName += (displayName != "") ? " " : "";
        if (instanceOnly && displayName == "") { return; }

        var className:String = getCleanClassName(displayObject);

        var text:String = (className == "TextField") ? String(" \"" + TextField(displayObject).text) + "\"" : ""

        var objectName:String = displayName + className + text;

        var dataString:String = (withData) ? String(" [x:" + displayObject.x + " y:" + displayObject.y + " width:" + displayObject.width + " height:" + displayObject.height + "]") : "";

        var parentName:String = (withParent && parent != "") ? String(" (" +  parent + ")") : "";   

        var levelStr:String = (withParent) ? String(currentDepth + " ") : "";

        trace(levelStr + levelSpace + objectName + dataString + parentName);

        if (displayObject is DisplayObjectContainer && currentDepth < maxDepth)
        {       
            for (var i:int = 0; i < DisplayObjectContainer(displayObject).numChildren; i++)
            {
                traceDisplayList(DisplayObjectContainer(displayObject).getChildAt(i), instanceOnly, withData, withParent, maxDepth, skipClass, levelSpace + "    ", currentDepth + 1, objectName);
            }
        }
    }   

    public static function getCleanClassName(obj:*):String 
    {
        var className:String = String(getQualifiedClassName(obj));
        var indexOfString:int = className.lastIndexOf("::");
        className = (indexOfString < 0) ? className : className.slice(indexOfString + 2);
        return className;           
    }

    public static function displayObjectCleanName(displayObject:DisplayObject):String
    {
        return String((displayObject.name.substr(0, 8) == "instance") ? "" : displayObject.name);
    }



回答3:


I don't get this concept of finding maximum items recursively

public static int Get-Max(int[] List, int count)

{ int max=-500000;
   if(count<=0)
    return -1;

   else
    {
    if(List[x]>max)
    max=List[x];
    }
}


来源:https://stackoverflow.com/questions/17195452/how-to-trace-a-full-display-list-with-a-recursive-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!