释放CocosNode并将其子项添加到Cocos2d中的Layer

时间:2009-10-02 06:11:15

标签: iphone objective-c cocos2d-iphone

我正在为Cocos2d为iPhone制作一个俄罗斯方块克隆,我正在为各个块使用Block类Sprite,以及用户为了移动Blocks而控制的Tetromino CocosNode类。所有这些块在GameBoardLayer上以20×10网格的空块移动。

当块完成掉落时,我想释放Tetromino并将它的Block子连接到GameBoardLayer,以便让它们独立漫游并为用户创建一个新的Tetromino。

我尝试在图层中覆盖 removeChild:

- (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];
}

但似乎我无法将孩子加两次,因为它已经是Tetromino的Layer的孩子了。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

为什么不交换addChild和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];
}