I am quite new to Objective-C and Cocos2d, and i don’t really understand why i am getting the following error with the following code. Could you please help ?
- (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;
}
and i get the error:
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'
Sorry if this question is very low level… and thanks if you can help me ! 🙂
Your error is that you are trying to add created menuItem to two parents. add it to the menu only. Then add menu to the layer. Replace your line
with this one
and you will receive an array of pages for every player.