Iphone cocos2d动画后自动记忆精灵
我对cocos2d和iphone开发一无所知。我想创建一些动画,当一些带有精灵的物理对象被破坏时(例如显示飞溅)。我想做一个物体,我会说:运行动画,完成后摧毁自己。然后,我想忘记这个对象-它应该在动画完成时自动销毁。最好的方法是什么?您可以使用CCSequence创建操作列表。你做的第一个动作应该是你的常规动作(或顺序)。第二个操作应该是CCCallFuncND操作,您可以在其中调用清理函数并传递给定的精灵 在我脑海中,我会做这样的事情:Iphone cocos2d动画后自动记忆精灵,iphone,objective-c,c,cocos2d-iphone,Iphone,Objective C,C,Cocos2d Iphone,我对cocos2d和iphone开发一无所知。我想创建一些动画,当一些带有精灵的物理对象被破坏时(例如显示飞溅)。我想做一个物体,我会说:运行动画,完成后摧毁自己。然后,我想忘记这个对象-它应该在动画完成时自动销毁。最好的方法是什么?您可以使用CCSequence创建操作列表。你做的第一个动作应该是你的常规动作(或顺序)。第二个操作应该是CCCallFuncND操作,您可以在其中调用清理函数并传递给定的精灵 在我脑海中,我会做这样的事情: CCSprite* mySpriteToCleanup
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];
在清理功能中:
- (void) cleanupSprite:(CCSprite*)inSprite
{
// call your destroy particles here
// remove the sprite
[self removeChild:inSprite cleanup:YES];
}
您也可以在这两个动作之间为销毁粒子动作添加另一个动作,而不是在end函数中调用该动作。方便的方法是使用自定义
RemoveNode
动作,该动作删除正在运行的CCNode
对象(CCSprite
也是CCNode
)
将其置于CCSequence last参数中。例如,在淡出后将删除精灵:
[mySprite runAction:[CCSequence actions:
[CCFadeOut actionWithDuration:0.5], [RemoveNode action], nil]];
很不错的。在COCOS2D2.0中,您还可以使用块:
CCAction*cleanupAction=[CCCallBlock actionWithBlock:^{[MyPriteToCleanup removeFromParentAndCleanup:YES];}]在CoCoS2D2.1中,我不得不将其调整为[(CCNode*)\u target removeFromParentAndCleanup:YES]代码>
[mySprite runAction:[CCSequence actions:
[CCFadeOut actionWithDuration:0.5], [RemoveNode action], nil]];