我曾经在运行新操作时停止所有操作。我在使用CCTintBy操作时注意到了一些问题,因为颜色正在逐渐添加并且对“反向”的调用(参见 CCTintBy (CCActionInterval*) 反向函数)没有被调用(如停止)。
这让我想知道我目前的方法和对 CCActions 的理解。
例如。每当运行一个新动作时,我都会调用 [self stopAllActions] 但这并不适合 CCTintBy 动作的使用(它也会停止该动作并使精灵变为半色,并且底色会改变为相反函数不会被 stopAllActions 停止调用)。
我留给你我项目中最常见的操作。我在想而不是调用 stopAllActions,如果该操作尚未运行,则仅调用停止特定操作。这会是一个好习惯吗?
-(void) runExplosionAnimation
{
[self stopAllActions];
//here should verify if the action is not already running and if so stop it
//To do so I should have a member variable like: CCAction * explosionAnim = [CCSequence actions: [CCAnimate actionWithDuration:0.4f animation:anim restoreOriginalFrame:false], [CCHide action], nil];
//Plus a boolean to distinguish if it is already running..
CCAnimation* anim = [[CCAnimationCache sharedAnimationCache] animationByName:@"bbb"];
if(anim!=nil){
[self runAction:[CCSequence actions: [CCAnimate actionWithDuration:0.4f animation:anim restoreOriginalFrame:false], [CCHide action], nil]];
}
else{
[self loadSharedAnimationIfNeeded];
}
}
使用布尔值确定 CCTintBy 操作是否仍在运行的一种方法是在再次调用 CCTintBy 操作之前手动恢复原始颜色。
-(void) gotHitWithFactor:(int)factor
{
[self runGotHitAnimation];
self.hitPoints -= factor * 1;
if (self.hitPoints <= 0)
{
isAlive=false;
[self runExplosionAnimation]; //Will also set enemy visibility to false
}
}
-(void) runGotHitAnimation
{
//hitAction is initialized as [CCTintBy actionWithDuration:0.1f red:100 green:100 blue:111];
[self stopAction:hitAction];
self.color = originalColour; //Where originalcolour is initialized as self.colour
[self runAction:hitAction];
}