我想查看特定DisplayObjectContainer的显示列表中的所有实例。如何使用递归函数来超越第一个深度?
问问题
1016 次
3 回答
2
DisplayObject
这是最简单的代码,您可以递归地查看从上到下的所有父级。添加numChildren
调整,您也可以看到父母的所有其他孩子。
function traceD(mc:DisplayObject) {
trace(mc);
if(mc.parent!=null) traceD(mc.parent);
}
于 2013-06-20T12:36:42.207 回答
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
(使用其他两个功能)。如果对象是 a ,您将在侧面看到文本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);
}
于 2013-06-19T15:36:36.060 回答
-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];
}
}
于 2014-06-05T14:45:12.950 回答