Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use CCSpriteBatchNode?

in Cocos2d I will be playing an animation. The animation has about 12 frames, and each frame is rather big. In fact, the -hd version of each frame is quite huge.

Anyway, first, I created it by putting all 12 frames in a texture using Zwoptex. The texture is about 2048x2048.

This is so I can animate a CCSprite in a CCSpriteBatchNode using that texture.

But I seem to be getting a level 2 memory warning.

Now that I think of it, I don't think CCSpriteBatchNode was supposed to be used for one sprite. I guess it was only useful if you wanted to draw lots of sprites that use the same texture.

So I want to know: Should I animate the sprite frame by frame (no huge texture)? Or is it possible to use that huge texture but in a different way?

like image 883
Voldemort Avatar asked Sep 25 '11 23:09

Voldemort


1 Answers

You are right about CCSpriteBatchNode. CCSpriteBatchNode is like a batch node: if it contains children, it will draw them in 1 single OpenGL call (often known as "batch draw"), in absence of CCSpriteBatchNode (in this case) all the "batch draw" will be called as many time as number of children (sprites).

A CCSpriteBatchNode can reference one and only one texture (one image file, one texture atlas) i.e. sprite sheet created by zwoptex. Only the CCSprites that are contained in that texture can be added to the CCSpriteBatchNode. All CCSprites added to a CCSpriteBatchNode are drawn in one OpenGL ES draw call. If the CCSprites are not added to a CCSpriteBatchNode then an OpenGL ES draw call will be needed for each one, which is less efficient.

According to your scenario, you don't have to use CCSpriteBatchNode since there is only one texture rendered at any given time.

So I want to know: Should I animate the sprite frame by frame (no huge texture)? Or is it possible to use that huge texture but in a different way?

It doesn't matter. You will be loading the 2048 x 2048 texture anyways. The issue to ponder is why only one 2048 x 2048 texture is giving you a Level 2 warning? how many such textures are you loading. BTW 2048 x 2048 is only supported on iPod3G/iPhone3GS above (which is fine).

In case you are loading many textures (which seems true to me). You need to program some logic where you can unload the texture when they are not necessary. Do look at the following methods:

[[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFramesFromFile:fileName];
[[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];
[[CCTextureCache sharedTextureCache] removeUnusedTextures];

As far as animation is concerned you can create CCAnimation and use that or (depending upon your scenario) you can use setDisplayFrame(CCSpriteFrame frame);

like image 74
Asymptote Avatar answered Sep 21 '22 23:09

Asymptote