【问题标题】:Sprite animation and remove精灵动画和移除
【发布时间】:2012-11-27 14:47:34
【问题描述】:

我想在游戏中的松鼠英雄触摸水果时为其设置动画。动画后必须将水果从场景中移除。我已经尝试了 2 周,但没有做对。我的精灵表有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;
}

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

【问题讨论】:

  • 看this awesome code,看看猴子碰到香蕉时的动画逻辑。 :)
  • 我没有使用任何物理编辑器。
  • 您实际上有什么问题?您无法检测到松鼠与水果的碰撞,或者您不知道动画完成后如何移除精灵?
  • 碰撞效果很好,动画和移除是问题。
  • @SpencerWong 它是从 tilemap 检索到的水果位置。

标签: iphone animation cocos2d-iphone sprite


【解决方案1】:

如果你想从你的游戏层中完全移除精灵,你可以将类别添加到 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];
}

【讨论】:

  • 这不是删除的问题,即使不删除,动画也无法正常工作。请帮助我处理动画部分。
  • 确保在调用 [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"Fruit01000%d.png", i]]之前将图集中的帧添加到 spriteFrame 缓存中
  • 另外,正如我所见,你使用单个节点来运行所有的爆炸动画。调用 stopAllActions 是错误的。因此,如果您在上一个动画期间尝试运行它,之前启动的动画将停止并在下一个地方运行。
  • 请给出解决方案。
  • 应用程序在移动到 levelUp 场景时崩溃,出现错误“未分配指针被释放”
猜你喜欢
  • 1970-01-01
  • 2011-12-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
  • 2023-03-30
  • 1970-01-01
  • 2015-05-20
相关资源
最近更新 更多