CCNode递归getChildByTag [英] CCNode recursive getChildByTag
问题描述
据我所知, CCNode :: getChildByTag
方法只在直接子女之间搜索。
As far as I understand the CCNode::getChildByTag
method only searches among direct children.
有什么办法递归地找到一个CCNode的子标签在其所有的后代层次结构?
But is there any way to recursively find a CCNode's child by tag in all its descendant hierarchy ?
我从CocosBuilder ccb文件加载一个CCNode,我想检索
I'm loading a CCNode from a CocosBuilder ccb file and I'd like to retrieve subnodes knowing only their tags (not their position/level in the hierarchy)
推荐答案
一种方法 - 创建自己的方法。或者使用此方法为CCNode创建类别。它看起来像这样的smth
One way - to create your own method. Or create category for CCNode with this method. It will look like smth like this
- (CCNode*) getChildBuTagRecursive:(int) tag
{
CCNode* result = [self getChildByTag:tag];
if( result == nil )
{
for(CCNode* child in [self children])
{
result = [child getChildByTagRecursive:tag];
if( result != nil )
{
break;
}
}
}
return result;
}
将此方法添加到CCNode类别。您可以在任何您想要的文件中创建类别,但我建议只创建此类别的单独文件。在这种情况下,任何其他对象,其中这个头将被导入,将能够发送此消息到任何CCNode子类。
Add this method to the CCNode category. You can create category in any file you want but I recommend to create separate file with this category only. In this case any other object where this header will be imported, will be able to send this message to any CCNode subclass.
实际上,任何对象都能够发送此消息,但是在导入头文件时,它会在编译期间产生警告。
Actually, any object will be able to send this message but it will cause warnings during compilation in case of not importing header.
这篇关于CCNode递归getChildByTag的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!