Search code examples
objective-ccocos2d-iphone

An explanation with Cocos2D method


I just started with cocos2D and came across something that looks like this ..

CCSprite *sprite = (CCSprite *)[self getChildByTag:13];

then I can just simply do stuff with the object.

I'm really curious with what's happening after the ' = ' part of the code.

I know that the [self getChildByTag:13] retrieves the object by the tag I assigned to it in a previous method, the -(id)init method, and I know that by it self (CCSprite *) is just a pointer to something that will be a member of it self.. but how do these two things work together.


Solution

  • Basically you are fetching a CCSprite object in a scene by requesting the "child" via tag.

    The getChildByTag method retrieves the node (or CCSprite in this case... you may want to put in a bit of validation code to make sure what's retrieved truly is a CCSprite object) and then assigns it to your sprite variable (thanks to a cast you're doing in there).

    Oh, here's a related question that might help you out, too.