I have 2 scenes both the scene shares a same back ground Image so I have this below code in both scenes init method and added. Everything works prefect. My question is , since the same image file is read twice , allocated memory and deallocated so why cant we store it one common place (spriteManager) using singleton pattern and reuse that instance? If I do this way will the memory will be released when when the scene one get replaced with scene 2.
One this strike my mind is Retain but not sure how and where to handle this retain. If my approach is wrong for handling Common reusable sprite then please suggest me the proper way of doing the same. I am new to this cocos2d so I havent read the concept of sprite sheet , for now I want to keep everything simple.
CCSprite *bg = [CCSprite spriteWithFile:@"bg.png"];
2nd Question , I read while replacing two heavy scene there would be some chances of memory leak due to overlap in releasing first scene memory and allocating the next scene memory. So to avoid this I read we have to give a pause for seconds load a light weight scene (Loading scene). Is that a good solution or the replacing scene itself will not create any problems.
Would be great if there is a good article about this post title (Reusing Sprite & Replacing scene). .
To answer your first question, you don’t need a spriteManager because cocos2D already have a default texture caching mechanism(refer to ccTextureCache class), and when you call spriteWithFile next time it will just load the texture from texture cache. Note: ccTextureCache usually clear its textures when there is a memory warning.
Secondly there shouldn’t be any memory leaks when replacing scenes if you have deallocated the objects properly. Can you provide a link to where you read about this?