递归获取as3中阶段的所有子代、孙子等
这是我第一次在动作脚本中使用递归,所以我确信有些东西我没有考虑到。 我想做的只是迭代阶段子项并找出子项是什么和索引。这是一些代码。
public function recurseStage(dOC:DisplayObjectContainer)
{
var numCh = dOC.numChildren;
for(var i = 0; i < numCh; i++)
{
var child = dOC.getChildAt(i);
trace("child: " + child + " at i: " + i);
if(child.numChildren > 0)
{
recurseStage(child);
}
}
}
问题区域似乎是最后的实际 recurseStage() 调用。以及之前的 if 语句。我知道并非所有孩子都会拥有 .numChildren 属性,但我不确定使用什么来代替。这应该是一个简单的解决办法,但我的大脑现在无法帮助我。 另外,如果有比这更好的方法,请告诉我。提前致谢。
This is my first time using recursion in actionscript so I'm sure that there is something I'm not accounting for.
What I'm trying to do is just iterate through stage children and trace out what the child is and the index. Here is some code.
public function recurseStage(dOC:DisplayObjectContainer)
{
var numCh = dOC.numChildren;
for(var i = 0; i < numCh; i++)
{
var child = dOC.getChildAt(i);
trace("child: " + child + " at i: " + i);
if(child.numChildren > 0)
{
recurseStage(child);
}
}
}
the problem area seems to be the actual recurseStage() call at the end. As well as the if statement before that. I know not all children will have the property .numChildren but I'm not sure what to use instead. This should be an easy fix but my brain just isn't helping me out right now.
Also if there is a better way than this please let me know. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
if (child is DisplayObjectContainer && child.numChildren > 0)
而不是
if(child.numChildren > 0)
use
if (child is DisplayObjectContainer && child.numChildren > 0)
instead of
if(child.numChildren > 0)