我对cocos2d和iphone的开发都是新手。我想创建一些动画,当一些物理对象与它的精灵被摧毁(例如,以显示飞溅)。我想做一些我想说的东西:运行动画,完成后毁了你自己。然后我想忘记这个对象-它应该在动画结束时自动销毁。做这件事最好的方法是什么?
发布于 2010-11-14 06:49:40
您可以使用CCSequence创建操作列表。你做的第一个动作应该是你的常规动作(或序列)。第二个动作应该是CCCallFuncND动作,您可以调用一个清理函数并传递给定子画面。
在我的脑海中,我会这样做:
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函数中:
- (void) cleanupSprite:(CCSprite*)inSprite
{
// call your destroy particles here
// remove the sprite
[self removeChild:inSprite cleanup:YES];
}您可以在这两个操作之间添加另一个操作,用于销毁粒子操作,而不是在end函数中调用该操作。
发布于 2012-12-21 23:34:01
更方便的方法是使用自定义的RemoveNode操作,该操作删除正在运行的CCNode对象(CCSprite也是CCNode)。
//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将在淡出后被移除:
[mySprite runAction:[CCSequence actions:
[CCFadeOut actionWithDuration:0.5], [RemoveNode action], nil]];https://stackoverflow.com/questions/4174445
复制相似问题