Using the sprite frame cache in cocos2d is the right way

I use a sprite cache frame to load plists and sprite sheets for use in animations. I have 2 approaches to this, and I'm sure one of them is wrong .

  • I need to download everything that I can to cache at the beginning, because it takes time, so at the beginning of the scene I load everything I need in the near future. (but now my cache is full!)

  • the cache should remain as empty as I can, so I load into the cache the moment I start the animation (it takes some time, right? I think it clicks my game) and delete it the very moment I finish with this. (Now the cache is not full, but I have to repeatedly load / reload.)

  • do i have to take care to remove unused sprites from cache every time?

Cache loading:

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

removal of unused:

 [[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames]; 

I see on my iPad that the system is trying to remove unused sprites and there are problems. how to work?

+6
source share
1 answer

After many studies, I came to some conclusions:

Firstly, it takes time to load frames in real time (when starting an animation), and in the case of large images (iPad) it sometimes freezes the scene.

So, in some cases, you MUST load the images in the cache with the init method to avoid this, because you have no other way, and therefore we have the cache anyway:

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

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

Other things should be removed from the cache if you are not using it.

 [[CCTextureCache sharedTextureCache] removeUnusedTextures]; 

clears cache only sprites that have already been removed from the screen.

it's not enough to just clear frames:

 [[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames]; 

In other scenarios where the sprite images are not so large, you can load them into batchNode at the moment the animation starts and delete them at the end.

+3
source

Source: https://habr.com/ru/post/951782/


All Articles