2

我在 cocos2dx 中实现了一个应用程序。

我目前面临的问题是我无法找到孩子是精灵还是图层,因为 getChildren() 方法返回 CCObjects 列表。

任何帮助表示赞赏。

4

2 回答 2

6

当你有一个孩子时,你需要做一个类型转换来检查它是一个精灵还是一个图层:

for(int i = 0; i < myNode->getChildren()->count(); i++)
{
    CCNode *child = myNode->getChildren()->objectAtIndex(i);
    CCSprite* s = dynamic_cast<CCSprite*>(child);
    if(s != 0) {
        ...
    }
}
于 2013-03-14T13:50:26.770 回答
1

这是另一个示例可能会有所帮助:

Vector<Node*> allNodes=this->getChildren();
for(auto&  node : allNodes){
    if(dynamic_cast<Sprite*>(node)){ //It is Sprite 
        Sprite *target=dynamic_cast<Sprite*>(node);
        //Do whatever you like
    }
}
于 2015-11-23T10:15:56.607 回答