Search code examples
ioscocos2d-iphone

CCNode remove not working Cocos2d


For some reason my remove function on my CCNode is not working after the animation is run.

I am adding a CCNode on click, running a animation then running the remove function.

         // loads the Coin
         CCNode* coin = [CCBReader load:@"heros/coin"];
         coin.name =@"coin";
         // position
         coin.position = ccpAdd(_touchNode.position, ccp(0, 0));

         //Add to Parent
         [_touchNode addChild:coin z:-1];

         id action1 = [CCActionMoveTo actionWithDuration:0.7f position:ccpAdd(_touchNode.position, ccp(0, 200))];
         id action2 = [CCActionMoveTo actionWithDuration:0.7f position:ccpAdd(_touchNode.position, ccp(0, 100))];
         CCActionCallFunc *callAfterMoving = [CCActionCallFunc actionWithTarget:self selector:@selector(cleanupSprite:)];
         [coin runAction: [CCActionSequence actions:action1, action2, callAfterMoving, nil]];

using the following delete function produces a crash error with This node does not contain the specified child

- (void) cleanupSprite:(CCNode*)inSprite
{
    // call your destroy particles here
    // remove the sprite
    [self removeChild:inSprite cleanup:YES];
}

Using the following delete also doesn't work

- (void) cleanupSprite:(CCNode*)inSprite
{
    // call your destroy particles here
    // remove the sprite
    [self removeChildByName:@"coin" cleanup:YES];
}

Solution

  • self does not contain coin, _touchNode does. Do this in your callback :

    [_touchNode removeChildByName:@"coin" cleanup:YES];