如何在as3中的DisplayObjectContainer中遍历所有子节点?我想要这样的语法:
for each(var displayObject:DisplayObject in displayObjectContainer )
{
displayObject.x += 10;
displayObject.y += 10;
}
答案 0 :(得分:13)
不确定每个是否有效,但这都有效。
for (var i:int = 0; i<myObj.numChildren; i++)
{
trace(myObj.getChildAt(i));
}
答案 1 :(得分:2)
这样的事可能吗?
function getChildren(target:DisplayObjectContainer):Array {
var count:uint = target.numChildren;
var ret:Array = [];
for (var i:int = 0; i < count; i++)
ret.push(target.getChildAt(0));
return ret;
}
然后
for each (var child:Array in getChildren(displayObjectContainer)) {
//....
}
格尔茨
back2dos
答案 2 :(得分:2)
您可以使用以下递归函数来遍历任何DisplayObjectContainer类的所有子项。
function getChildren(dsObject:DisplayObjectContainer, iDepth:int = 0):void
{
var i:int = 0;
var sDummyTabs:String = "";
var dsoChild:DisplayObject;
for (i ; i < iDepth ; i++)
sDummyTabs += "\t";
trace(sDummyTabs + dsObject);
for (i = 0; i < dsObject.numChildren ; ++i)
{
dsoChild = dsObject.getChildAt(i);
if (dsoChild is DisplayObjectContainer && 0 < DisplayObjectContainer(dsoChild).numChildren)
getChildren(dsoChild as DisplayObjectContainer,++iDepth);
else
trace(sDummyTabs + "\t" + dsoChild);
}
}
它将以分层方式显示所有子项,与DisplayList树完全一样。
答案 3 :(得分:0)
我的两分钱。
public static function traceDisplayList(displayObject:DisplayObject, maxDepth:int = 100, skipClass:Class = null, levelSpace:String = " ", currentDepth:int = 0):void
{
if (skipClass != null) if (displayObject is skipClass) return;
trace(levelSpace + displayObject.name); // or any function that clean instance name
if (displayObject is DisplayObjectContainer && currentDepth < maxDepth)
{
for (var i:int = 0; i < DisplayObjectContainer(displayObject).numChildren; i++)
{
traceDisplayList(DisplayObjectContainer(displayObject).getChildAt(i), maxDepth, skipClass, levelSpace + " ", currentDepth + 1);
}
}
}