为什么实现回调(通过目标/选择器或块)到我的CCSequence崩溃我的应用程序?

时间:2012-08-21 00:51:24

标签: objective-c cocos2d-iphone

我试图在精灵本身完成动画后从我的主游戏层中删除一个精灵...要做到这一点,我首先尝试将一个块传递给精灵对象的CCSequence,如下所示:

#Game.m
// some method
    [self.spriteMan zoomAwayWithBlock:{[self destroySpriteMan];}];
    [self createNewSpriteMan];
}

-(void)destroySpriteMan {
    [self removeChild:self.spriteMan cleanup:YES];
}

#SpriteMan.m
-(void)zoomAwayWithBlock:(void(^)())block {
    [self runAction:[CCSequence actions: [CCScaleTo actionWithDuration:2.0f scale:1.0f],
                     [CCCallFuncN actionWithBlock:block],
                     nil]];
}

我想知道如果以某种方式将self.spriteMan的绑定搞砸到动画完成之前调用的[self createNewSpriteMan] ....所以我在调用它之前将spriteMan存储在tempSpriteMan变量中,并尝试tempSpriteMan上的removeChild .....都会立即导致崩溃。

然后我重写了这个以使用选择器和目标:

#game.m
[self.spriteMan zoomAwayWithSelector:@selector(destroySpriteMan:) target:self];

-(void)destroySpriteMan:(SpriteMan *)spriteMan {
    [self removeChild:spriteMan cleanup:YES];
}

#SpriteMan.m
-(void)zoomAwayWithSelector:(SEL)sel target:(id)target {
    [self runAction:[CCSequence actions: [CCScaleTo actionWithDuration:2.0f scale:1.0f],
                     [CCCallFuncN actionWithTarget:target selector:sel],
                     nil]];
}

同样的结果......每次都崩溃......我做错了什么?

2 个答案:

答案 0 :(得分:1)

正如Aroth指出的那样,答案就在于这个主题: http://cocos2d-iphone.org/forum/topic/6818

它显示了问题的解决方案,将其放入行动数组中为我解决了问题:

[CCCallFuncO actionWithTarget:self selector:@selector(removeFromParentAndCleanup:) object:[CCNode node]].

答案 1 :(得分:0)

您可以尝试使用方法

将类别添加到CCNode
- (void) removeFromParentWithCleanupYes
{
    [self removeFromParentWithCleanup:YES];
}

然后只是制作这样的序列

id sequence = [CCSequence actions: action1, 
                                   action2, 
                                   ..., 
                                   [CCCallFunc actionWithTarget: target selector:@selector(removeFromParentWithCleanupYes)], 
                                   nil];
[target runAction: sequence];

它不是很优雅,但应该有效。

或者你可以创建自己的简单动作,从父母那里删除它的目标。