Is there perhaps some best practice to caching CCAnimations in Cocos2D? Currently I am basically recalculating an “appear” animation for a sprite every time it appears on the screen. Wouldn’t it be wise to cache the animation in the instance class somehow?
Currently, I run this code like thousands of times for some given game conditions:
CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
[frameCache addSpriteFramesWithFile:@"WichserAnimations.plist"];
NSMutableArray* frames = [NSMutableArray arrayWithCapacity:8];
for (int i = 41; i <= 48; i++) {
NSString* file = [NSString stringWithFormat:@"intro_prep__Main_000%i.png", i];
CCSpriteFrame* frame = [frameCache spriteFrameByName:file];
[frames addObject:frame];
}
CCAnimation* anim = [CCAnimation animationWithSpriteFrames:frames delay:0.05f];
CCAnimate* connect = [CCAnimate actionWithAnimation:anim];
[self runAction:connect];
I thought of putting the “connect” CCAnimate object into the sprite for later re-use, but this does not seem to work 🙁 any advice will be highly appreciated
The animation cache class is called
CCAnimationCache.