views:

348

answers:

1

I'm working on a Tetris clone in Cocos2d for the iPhone and I'm using a Block class of Sprites for the individual blocks and a Tetromino CocosNode class that the user controls in order to move the Blocks. All of these Blocks move around in a 20 by 10 grid of empty Blocks on the GameBoardLayer.

When the block finishes falling, I'd like to release the Tetromino and attach it's Block children to the GameBoardLayer in order to let them roam independently and create a new Tetromino for the user.

I tried overriding removeChild: in the Layer:

- (void)removeChild: (CocosNode*)child cleanup:(BOOL)cleanup
{
    if ([child isEqual:userTetromino]) {
     for (Block *currentBlock in userTetromino.children) {
      [self addChild:currentBlock];
      [userTetromino removeChild:currentBlock cleanup:YES];


     }
    }

    [super removeChild:child cleanup:cleanup];
}

But it seems I cant add the child twice, since it's already a child of Layer through Tetromino. Any thoughts?

+1  A: 

Why don't you swap the addChild and the removeChild?

- (void)removeChild: (CocosNode*)child cleanup:(BOOL)cleanup
{
    if ([child isEqual:userTetromino]) {
        for (Block *currentBlock in userTetromino.children) {
                // The following lines are swapped here.
                [userTetromino removeChild:currentBlock cleanup:YES];
                [self addChild:currentBlock];


        }
    }

    [super removeChild:child cleanup:cleanup];
}
Jeff B