首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >cocos2d动画后自动删除精灵

cocos2d动画后自动删除精灵
EN

Stack Overflow用户
提问于 2010-11-14 04:08:16
回答 2查看 8K关注 0票数 3

我对cocos2d和iphone的开发都是新手。我想创建一些动画,当一些物理对象与它的精灵被摧毁(例如,以显示飞溅)。我想做一些我想说的东西:运行动画,完成后毁了你自己。然后我想忘记这个对象-它应该在动画结束时自动销毁。做这件事最好的方法是什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-11-14 06:49:40

您可以使用CCSequence创建操作列表。你做的第一个动作应该是你的常规动作(或序列)。第二个动作应该是CCCallFuncND动作,您可以调用一个清理函数并传递给定子画面。

在我的脑海中,我会这样做:

代码语言:javascript
复制
CCSprite* mySpriteToCleanup = [CCSprite spriteWithFile:@"mySprite.png"];
[self addChild:mySpriteToCleanup];

// ... do stuff

// start the destroy process
id action1 = [CCIntervalAction actionWithDuration:0];  // the action it sounds like you have written above.
id cleanupAction = [CCCallFuncND actionWithTarget:self selector:@selector(cleanupSprite:) data:mySpriteToCleanup];
id seq = [CCSequence actions:action1, cleanupAction, nil];
[mySpriteToCleanup runAction:seq];

在cleanup函数中:

代码语言:javascript
复制
- (void) cleanupSprite:(CCSprite*)inSprite
{
    // call your destroy particles here
    // remove the sprite
    [self removeChild:inSprite cleanup:YES];
}

您可以在这两个操作之间添加另一个操作,用于销毁粒子操作,而不是在end函数中调用该操作。

票数 8
EN

Stack Overflow用户

发布于 2012-12-21 23:34:01

更方便的方法是使用自定义的RemoveNode操作,该操作删除正在运行的CCNode对象(CCSprite也是CCNode)。

代码语言:javascript
复制
//Remove the node from parent and cleanup
@interface RemoveNode : CCActionInstant
{}
@end

@implementation RemoveNode
-(void) startWithTarget:(id)aTarget
{
    [super startWithTarget:aTarget];
    [((CCNode *)target_) removeFromParentAndCleanup:YES];
}

@end

将其放在CCSequence的最后一个参数中。例如,sprite将在淡出后被移除:

代码语言:javascript
复制
[mySprite runAction:[CCSequence actions:
[CCFadeOut actionWithDuration:0.5], [RemoveNode action], nil]];
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4174445

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档