2012-06-15 60 views
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' 

很抱歉,如果這個問題是非常低的水平......和感謝,如果你能幫幫我! :)

回答

1

你的錯誤是,你正試圖添加到兩個父母創建menuItem。僅將它添加到菜單中。然後將菜單添加到圖層。這一個

[newPage addChild:menu]; 

更換你行

[newPage addChild:titem]; 

,您將收到的每一個玩家的網頁的數組。

+0

非常感謝你,對於這個問題感到抱歉,現在我意識到我應該找到那個......再次感謝! –

+0

您可以將回答標記爲有效,以幫助他人找出這類問題的解決方案=) – Morion