如何使用递归函数跟踪完整的显示列表?

时间:2013-06-19 15:36:36

标签: actionscript-3 actionscript

我希望在显示列表中看到特定DisplayObjectContainer的所有实例。如何使用递归函数来超越第一个深度?

3 个答案:

答案 0 :(得分:2)

这是最简单的代码,您可以递归地查看DisplayObject的所有父代 从下到上。添加numChildren调整,您也可以看到父母的所有其他孩子。

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

答案 1 :(得分:0)

这是一个跟踪显示列表的简单递归函数。在MovieClip中,此行:traceDisplayL(this, 2);可以输出:

 root1 MainMe
     Alone Symbol1
         Shape
     BigOne Symbol2
         Shape
         InBigOne Symbol3

导入部分:

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

traceDisplayL函数:(不要使用currentDepth参数,保持原样)

    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);
            }
        }
    }

更精细的traceDisplayList(使用其他两个功能)。如果对象是TextField,您将看到侧面的文本,如果您想:

  • 只有“实例”对象(有名字的人);
  • 与对象相关的数据(.x,.y,.width,.height);
  • 父对象名称,在对象名称之后;

您还可以限制最大深度,和/或跳过特定班级的所有对象(如果您不希望在跟踪中看到任何Shape或无论什么)。

    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);
    }

答案 2 :(得分:-1)

我没有得到递归找到最大项目的概念

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];
    }
}