1

我想在游戏中的松鼠英雄触摸水果时为其设置动画。动画后必须将水果从场景中移除。我已经尝试了 2 周,但没有做对。我的 spritesheet 有 6 帧精灵动画。

目前,当英雄与水果相交时,我正在执行以下代码:

            NSMutableArray *burst = [NSMutableArray array];

            for(int i = 1; i <= 6; ++i) {
              [burst addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"Fruit01000%d.png", i]]];
            }

            CCAnimation *walkAnim = [CCAnimation animationWithFrames:burst delay:0.02f];
            CCActionInterval *animAction = [CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO];


            [expl stopAllActions];
            expl.visible = TRUE;
            expl.position = ccp(coinColl.position.x,coinColl.position.y);

            cleanupAction = [CCCallFuncND actionWithTarget:self selector:@selector(explOver:) data:expl];
            seqF = [CCSequence actions:animAction, cleanupAction, nil];


            [expl runAction:seqF];     


            [platformLayer removeChild:coinColl cleanup:YES];
            [self sumScore]; 
            [coinLabel setString: [NSString stringWithFormat:@"%i",appDelegate.coinScore+appDelegate.levelScore]];

explOver() 函数:

-(void) explOver:(CCSprite*)explosion{
    explosion.visible = FALSE;
}

它部分工作,一些水果不显示动画,并且跳转到下一个场景时应用程序崩溃,给出错误“指针被释放未分配”

4

1 回答 1

0

如果你想从你的游戏层中完全移除精灵,你可以将类别添加到 CCNode 类中作为这个。

@interface CCNode(CategoryName)
- (void) removeFromParentAndCleanupYES;
@end

@implementation CCNode(CategoryName)
- (void) removeFromParentAndCleanupYES
{
    [self removeFromParentAndCleanup:YES];
}
@end

然后您可以在使用序列运行任何操作后添加此方法调用:

id animate = // any action, CCAnimate in your case
id callback = [CCCallFunc actionWithTarget:_fruitSprite selector:@selector(removeFromParentAndCleanupYES)];
id sequence = [CCSequence actionOne: animate two: callback];
[_fruitSprite runAction: sequence];

这只是一个建议,但它应该可以正常工作。如果您需要在代码的多个部分中使用操作删除节点,这是有道理的。


另一个变体是使用 CCCallFuncN 动作,它将你的精灵作为参数传递给给定的选择器。

id animate = // any action, CCAnimate in your case
id callback = [CCCallFuncN actionWithTarget:self selector:@selector(onAnimationDidFinished:)];
id sequence = [CCSequence actionOne: animate two: callback];
[_fruitSprite runAction: sequence];


- (void) onAnimationDidFinished:(CCNode*)node
{
    [node removeFromParentAndCleanup:YES];
}
于 2012-12-10T10:59:32.453 回答