0

I've nsmutablearray with levels and two buttons. Next level and try again. When user touched next level, I set objectAtIndex:indexCurrLevel+1 and next level is loading without problem. But if user touched try again and I try to set level to objectAtIndex:indexCurrLevel app crashed with children already added error. That is crazy because if I set manually try again for ex. objectAtIndex:5 works perfectly until user don't playing on 5 level, because app crashed.

For index different than current level index, works perfectly. (objectAtIndex:index - works) manually set index gave the same reason as with index.

[self removeFromParentAndCleanup:YES];

    Levels *l = [levels2 objectAtIndex:index-1];
    Game *hl = [[Game alloc]initWithObstacles:[l staticO] :[l rotateO]:[l lvl]:[l level]:[l pendulumO]:self.levelsArray];
    [hl setLevels2:self.levels2];
    [hl setBasketY:[l basketY]];
    [hl setBasketX:[l basketX]];


    [l release];

    [[CCDirector sharedDirector] replaceScene:(CCScene*) hl];    
    [hl configureLevel];
    [hl release];

Eroor: Assertion failure in -[Game addChild:z:tag:], ../libs/cocos2d/CCNode.m:388 2012-05-11 19:03:20.349 Game[932:10a03] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'child already added. It can't be added again' * First throw call stack:

PawelC
  • 1
  • 2

3 Answers3

0

Guessing an array-bounds issue, but it's impossible to verify without a code sample or crash log.

Jake
  • 3,973
  • 24
  • 36
0

Try removing the statement [l release]. In general you should only release an object on which you have called retain or else which you have obtained using a method that begins with init, new or copy. The array levels2 will also be retaining each level, and it may be appropriate to leave it as the levels' owner. Run the static analyser in Xcode to pick up these issues.

Danyal Aytekin
  • 4,106
  • 3
  • 36
  • 44
0

Read the message : you are trying to addChild an object which already has a parent ... ie has already been added as a child of a CCNode descendant somewhere else in your code base. Line 388 of CCNode is an NSAssert, not breakable. Change CCNode temporarily to have a breakable instruction, as follows:

if (nil==child) {
    CCLOG(@"%@<addChild> : have nil child. not adding.",self.class);
    return;
}
if (child.parent) {
    CCLOG(@"%@<addChild> : This child is already added somewhere. not adding.",self.class); // **** PUT BREAKPOINT HERE *****//
    return;
}

NSAssert( child != nil, @"Argument must be non-nil"); // was line 388
NSAssert( child.parent == nil, @"child already added. It can't be added again");

the last 2 lines are from CCNode (in my project). If the error you mention occurs, i would put a break at the line indicated, rerun, and look at the stack trace. When you are done with your dev/test cycle, and such things matter to you, remember to restore cocos2d to its original state for such temporary mods.

YvesLeBorg
  • 9,070
  • 8
  • 35
  • 48