0

我对 Objective-C 和 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'

对不起,如果这个问题非常低级......如果你能帮助我,谢谢!:)

4

1 回答 1

1

您的错误是您试图将创建的 menuItem 添加到两个父母。仅将其添加到菜单中。然后将菜单添加到图层。更换你的线路

[newPage addChild:titem];

有了这个

[newPage addChild:menu];

您将收到每个玩家的一系列页面。

于 2012-06-15T15:25:12.897 回答