0
我對Objective-C和Cocos2d頗爲陌生,並且我不明白爲什麼我在下面的代碼中出現以下錯誤。能否請你幫忙 ?Cocos2d在循環中添加子項:不能添加子項
- (NSArray *) menuItemsArray
{
NSMutableArray *array = [NSMutableArray alloc];
for (Player * player in playersArray) {
NSLog(@"creating menu item with image %@",player.imageFilename);
CCLayer *newPage = [[CCLayer alloc] init];
CCMenuItemImage *titem = [CCMenuItemImage itemFromNormalImage:player.imageFilename
selectedImage:player.imageFilename
target:self selector:@selector(playerSelected:)];
CCMenu *menu = [CCMenu menuWithItems: titem, nil];
menu.position = ccp(self.contentSize.width/2, self.contentSize.height/2);
NSLog(@"before adding item to layer");
//[titem removeFromParentWithCleanup:YES];
[newPage addChild:titem];
//[newPage release];
NSLog(@"after adding item to layer");
[array addObject:newPage];
/*
[titem release];*/
//[newPage release];
//[menu release];
}
return array;
}
和我得到的錯誤:
2012-06-15 17:13:11.003 Killer[3222:707] *** Assertion failure in -[CCLayer addChild:z:tag:], /Users/max/Documents/workspace/Killer/Killer/libs/cocos2d/CCNode.m:388
2012-06-15 17:13:11.006 Killer[3222:707] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'child already added. It can't be added again'
很抱歉,如果這個問題是非常低的水平......和感謝,如果你能幫幫我! :)
非常感謝你,對於這個問題感到抱歉,現在我意識到我應該找到那個......再次感謝! –
您可以將回答標記爲有效,以幫助他人找出這類問題的解決方案=) – Morion