I used to stopAllActions when running a new action. I noticed some problems when using the CCTintBy action as the colour was progressively being added and the call to “reverse” (see in CCTintBy the (CCActionInterval*) reverse function) was not being called (as stopped).
This made me wonder on my current approach and understanding of CCActions.
For instance. I used to call [self stopAllActions] whenever running a new action but this doesn’t quiet fit with the usage of CCTintBy action (it does stop also that action and leave the sprite being half coloured and the base colour would have changed as the reverse function would not have being called as being stopped by the stopAllActions).
I leave you with the most common actions in my project. I was thinking to instead of calling stopAllActions, to call only stop a specific action if the action is not already running. Would this be good practice?
-(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];
}
}
A way around to using a boolean to determien if the CCTintBy action is still running is to restore the original colour manually before calling the CCTintBy action again.
-(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];
}
You know you can tag actions, right?
The CCNode class has methods to:
If you run a new action, you just need to tell which other actions need to stop in each particular case. For complex action usage I recommend to split your actions logically into two groups: gameplay and visual.
Gameplay actions are all actions influencing gameplay, such as movement. Visual are just that, visual effects, like skewing, rotation, tinting. The point in this distinction is that it makes it easier to prioritize actions. Mainly, visual actions should never stop, run or otherwise influence gameplay actions.