[self removeChildByTag:1 cleanup:YES];
這將消除只有一個孩子 以下是完整的實施方法
-(void) removeChildByTag:(int)aTag cleanup:(BOOL)cleanup
{
NSAssert(aTag != kCCNodeTagInvalid, @"Invalid tag");
CCNode *child = [self getChildByTag:aTag]; //here it is simply getting a single chil
if (child == nil)
CCLOG(@"cocos2d: removeChildByTag: child not found!");
else
[self removeChild:child cleanup:cleanup];
}
-(CCNode*) getChildByTag:(int) aTag
{
NSAssert(aTag != kCCNodeTagInvalid, @"Invalid tag");
CCNode *node;
CCARRAY_FOREACH(children_, node){
if(node.tag == aTag)
return node; //as it finds the first child with the specified tag it will return
}
// not found
return nil;
}
那豈不是更好地調用[aChild removeFromParentAndCleanup:YES]而不是[parentNode removeChild之:achild清理:是],會嗎? – ChaosCoder 2011-03-30 08:48:22
親愛的我建議你使用API,因爲它是開源的。 這裏是removeFromParentAndCleanup的實現: - (無效)removeFromParentAndCleanup:(BOOL)清理 { \t [PARENT_ removeChild之:自我清理:清理]; } – 2011-03-30 08:49:54
哦,對。沒有看到。感謝您的回答! – ChaosCoder 2011-03-30 08:55:10