CCNode删除不工作的Cocos2d

时间:2014-06-06 10:19:34

标签: ios cocos2d-iphone

出于某种原因,我的CCNode上的删除功能在动画运行后无效。

我在点击时添加CCNode,运行动画然后运行删除功能。

         // 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]];

使用以下删除功能会导致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];
}

使用以下删除也不起作用

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

1 个答案:

答案 0 :(得分:1)

self不包含硬币,_touchNode可以。在回调中执行此操作:

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