imagecocos2d-iphoneccspritecclayercctexturecache

Cocos2d. Load and store images correctly?


There are a lot of answers for this question. But all of them are incorrect!

For example if I have created a CCLayer object with one CCSprite object. I have 3 textures and I want to switch between them on every touch.

For example I will use something similar to this:

link

I run this application in Simulator. Then I call a memory warning. Then I try to switch between images (textures). And I see that 2 of 3 images are deleted (except of the image was shown at the same time memory warning appeared).

I tried to use retain/release commands for CCSprite and ССTexture2D but they cause a situation when dealloc method of released object is never called.

So how to store them correctly? I want to store them at memory warning and release/remove them when the current layer is destroyed.


Solution

  • Store them in one texture atlas, created with Texture Packer. Then it's as simple as calling [Sprite setDisplayFrame:frameName] to switch the displayed texture.