Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the sprite frame cache in cocos2d-the right way

I am using the sprite frame cache to upload plists,and sprite-sheets,to use for animation . i have 2 approaches to that and i am sure that one of them is wrong .

  1. i have to load everything i can to cache at the start because it takes time, so at the start of the scene i load all what i need to the close future .(but now my cache is full! )

  2. cache should stay as empty as i can , so i am loading to cache at the moment i start the animation (it takes some time isn't it ? i think it flicks my game ) , and remove it at the same moment i am done with it . (now cache is not full but i have to load/reload many times.)

  3. do i have to take care to remove the unused sprites from cache every time ?

load to cache is :

[[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFrameByName:@"stopAnim.plist"];

removing unused is :

[[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];

i can see in my iPad that the system is trying to remove unused sprites and have problems. whats exactly the way to work ?

like image 739
Curnelious Avatar asked Aug 15 '13 06:08

Curnelious


1 Answers

After lots of research , i came to some conclusions :

First , it do takes time to load the frames in real time (while the animation is starting) to the batchNode , and in cases of big images (iPad) it sometimes freezing the scene .

So , in some cases like that ,you MUST load the images to cache at the init method to avoid that, because you have no other way,and this is why we have cache anyway :

[[CCTextureCache sharedTextureCache] addImage:@"the_heavy_image.png"];

and if this animation happens frequently , you don't want to clear it from the cache .

Other stuff should be cleared from cache if you dont use it

[[CCTextureCache sharedTextureCache] removeUnusedTextures];

will clear from cache only the sprites that was removed from screen already.

its not enough to just clear the frames :

[[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];

In other scenarios where sprites-sheets images are not that big, you can load them to the batchNode at the moment the animation is starts, and remove them at the end .

like image 133
Curnelious Avatar answered Dec 09 '22 07:12

Curnelious