Sandeep
Sandeep

Reputation: 687

Cocos2dx - How to find if child is sprite or layer?

I have implemented an application in cocos2dx.

The issue that i am facing currently is that i am not able to find whether the child is a sprite or a layer as the getChildren() method returns list of CCObjects.

Any help appreciated.

Upvotes: 2

Views: 5098

Answers (2)

elroy
elroy

Reputation: 21

This is another sample may help:

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
    }
}

Upvotes: 1

Lars Blumberg
Lars Blumberg

Reputation: 21371

When you have a child, you need to do a typecast in order to check whether it's a sprite or a layer:

for(int i = 0; i < myNode->getChildren()->count(); i++)
{
    CCNode *child = myNode->getChildren()->objectAtIndex(i);
    CCSprite* s = dynamic_cast<CCSprite*>(child);
    if(s != 0) {
        ...
    }
}

Upvotes: 6

Related Questions