精灵动画并删除

时间:2012-12-10 07:32:32

标签: iphone animation cocos2d-iphone sprite

我希望在我的游戏中被松鼠英雄触碰时动画水果。动画结束后,水果必须从场景中删除。我已经尝试了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;
}

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

1 个答案:

答案 0 :(得分: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];
}