Reputation: 44455
I have a few animated sprites in my cocos2d iPhone game. By animated sprite I mean a CCSprite that has a CCRepeatForever action run on it. The repeat action cycles through the frames of the animation which come from a png spritesheet and associated plist coordinates file.
While I have only a few unique animations I have many animated sprites with the same animation. For example I might have 30 animated soccer players running around the screen all running a CCRepeatForever action. What's an efficient way to pre-load and re-use the animations in this case?
My current approach is to create the repeat actions when the game starts-up. Then when I want to create a new animated sprite I run a copy of the pre-loaded repeat action on the sprite. I question whether this is very efficient since the animations sometime stutter when starting. All feedback is greatly appreciated! I'm open to pretty much any architecture/set-up.
Edit - more info: I'm wondering if "pre-copying" the actions could work? When the game loads I'd fill a queue with copies of the repeat action. Then I'd pop them off as needed. When the size of the queue is below some threshold I'd refill it on a background thread.
Upvotes: 1
Views: 356
Reputation: 64477
Make sure you are using a texture atlas, and preload all the textures with animations in them. Ideally you'll want to be using the .pvr.ccz format because it loads tremendously faster than PNG.
Everything else you do probably has very little to no effect on framerate. Stuttering is most likely caused by loading textures on demand, or the same regarding audio buffers (ie playing audio without preloading).
Don't worry about optimizing the (frequent) creation of CCRepeatForever with CCAnimate and whatever else you are using unless you've run the profiler to measure and confirm that that's exactly the problem you're having. I'm 99.9% confident that the stuttering issue is not related to creating & running actions.
Loading resources is by far the most time-consuming task you can run during gameplay, so it's crucial to preload all resource files. In your case, you really only need to make sure the textures are in memory.
Upvotes: 2
Reputation: 7850
For frame-by-frame animations use CCAnimation and CCAnimationCache.
Upvotes: 1