How do I remove a child later? (cocos2d)
I added 开发者_StackOverflow社区a child like this inside of a CCLayer:
[self addChild:object1];
Later on I want to remove that object from the children. Ummm so how do I do that? Thanks.
Your question leads me to believe you don't know the cocos2d API reference: http://www.cocos2d-iphone.org/api-ref/
To remove object1 simply use this:
[self removeChild:object1 cleanup:YES];
If you don't keep a reference of object1 around you can remove it by tag, which means you'll have to give it a tag first:
object1.tag = 123; // just any arbitrary number
[self addChild:object1];
To remove it:
[self removeChildByTag:123 cleanup:YES];
I've added this Q&A to my cocos2d FAQ, please find more details to this answer here: http://www.learn-cocos2d.com/knowledge-base/cocos2d-iphone-faq/learn-cocos2d-public-content/manual/cocos2d-general/14824-how-to-remove-a-child-from-the-nodescenelayer
Try the removeChild method?
精彩评论