0

I've a background in my app, but clicking on a button should change it and use a different one.

I cannot add all of them in the sharedTextureCache because each one has a size of >16MB in the cache, and I've 30 different backgrounds.

What's the best way for switching the background without a loading time? I don't want the user to wait when clicking that button.

Thanks

nano
  • 2,511
  • 4
  • 25
  • 42

3 Answers3

2

remove all unused data. or texture...

[[CCDirector sharedDirector] purgeCachedData];
[[CCTextureCache sharedTextureCache] removeAllTextures];
[CCTextureCache purgeSharedTextureCache];
[[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFrames];
[CCSpriteFrameCache purgeSharedSpriteFrameCache];
Ayaz
  • 1,398
  • 15
  • 29
1

If you know what background will be shown next, you can preload it beforehand. Each retina device can use about 100 megabytes of memory (about 5 spritesheets 2048x2048). In this case you begin to receive memory warnings but app will work stable. All preloads you can make asynchronously to shared texture cache. Just don't forget to clean up unused textures by calling

[[CCTextureCache sharedTextureCache] removeUnusedTextures];

to force unload of unnesessary textures.

Morion
  • 10,495
  • 1
  • 24
  • 33
  • Yes, that's what I'm doing, but the asynchronous preloads are consuming time, the rest of the app doesn't respond well until these async calls have totally finished. That's my main issue. What I want to do is, when users click on button, some new backgrounds are added to the cache asynchronously, but the app goes very slowly until these are added. – nano Sep 04 '12 at 12:21
  • very strange... are you sure you use `addImageAsync:target:selector:` to load your image in background? – Morion Sep 04 '12 at 12:30
  • I know, adding 4 backgrounds (2048 x 2048 @ 32 bpp => 16384 KB) in the main thread using addImageAsync is slowing down the performance of the app until they are completely added. :( – nano Sep 04 '12 at 14:27
  • Do you really need to load all of them? Maybe, it will be enough to load next background, then, after it will appear, unload previous and load next and so on and so forth? – Morion Sep 04 '12 at 14:39
  • Yes, that'd be okay too, but I still notice how the app slows down a bit. – nano Sep 04 '12 at 14:41
  • how and where would you call to a function that implements "unload previous and load next and so on and so forth" – nano Sep 04 '12 at 14:42
  • 1
    If i need to implement something like this, i will try to load first background and begin to preload next. When it is need to change background(you click a button, etc.), set preloaded image as new background, unload previous image and begin to preload next image... – Morion Sep 04 '12 at 14:49
  • "100 meters of memory" <-- how does that fit into such a small device? :) – CodeSmile Sep 04 '12 at 17:24
  • ouch =) sry, it was a hard working day) in russian computer slang we time to time use "meters" instead of "megabytes" =) – Morion Sep 04 '12 at 17:31
  • I've implemented this solution, but the animations and the CCParticleSnow in the app stops for a second until the 2 new backgrounds are loaded asynchronously. This cache is slowing down the app while big sprites are being added. – nano Sep 05 '12 at 08:50
0

The only way I managed to not slow down the app was with this line:

[_background setTexture: [[CCTexture2D alloc] initWithImage:...]]

When I try to use the shared Texture Cache, even asynchronously, the app slows down. :-O

nano
  • 2,511
  • 4
  • 25
  • 42