Appropriate use of CCTextureCache
-
11-06-2021 - |
Question
I'm currently creating a CCSprite
like this:
CCSprite *s = [CCSprite spriteWithFile:@"image.png"];
This sprite is the background image of a CCLayer
that's used relatively often. Is the following use of CCTextureCache
more efficient?
CCTexture2D *t = [[CCTextureCache sharedTextureCache] addImage:@"image.png"];
CCSprite *s = [CCSprite spriteWithTexture:t];
Solution
No. Internally, all methods that use an image as a texture (not just CCSprite) will add the texture to the CCTextureCache.
The only reason why you would want to use addImage directly is when you want to pre-load certain textures so that the first appearance of a node using that texture won't cause a lag during gameplay.
OTHER TIPS
First of all, if you look to the code of spriteWithFile: method, you will see that it adds image to the texture cache anyway if cannot find it there.
The second thing you must know, that if you store your art in atlases for reducing memory usage(for example, atlas 2048x2048 pixels with 20 different pictures), spriteWithTexture: will create sprite with whole huge atlas(2048x2048 pixels) texture.