Reputation: 313
For my Cocos2d game, I would like to have all the assets (texture atlases and sound files) to be loaded before the main game starts. To do this, I have created a layer within my main game scene and called it preloadLayer and added this to the scene as the top most layer.
the preloadLayer consists of 2 sprites, one covers the top half of the screen and one covers the bottom half of the screen. Once all the assets are loaded, I would like the two sprite sheets to move out of the screen to reveal the main game.
he preloadlayer will load the following:
The questions are:
For the texture atlas I am using the [CCSpriteFrameCache sharedSpriteFrameCache]
and for the font atlas, I am loading it using [CCLabelBMFont labelWithString:Str fntFile: file]
;
I would appreciate very much your help with this.
Upvotes: 2
Views: 1178
Reputation: 4215
You can preload images into the cache asynchronously, with a callback.
[[CCTextureCache sharedTextureCache] addImageAsync:imagePath target:self selector:@selector(imageLoaded:)];
The callback's signature:
-(void)imageLoaded:(CCTexture2D*)texture;
Configure your callbacks so that you can find out that your last image has loaded. Bear in mind that if you kick off more than one image load in this manner they may not complete in the order they were started.
Upvotes: 3
Reputation: 2201
use CCTextureCache
like so:
[[CCTextureCache sharedTextureCache] addImage:@"myImageName"];
and for sounds:
[[SimpleAudioEngine sharedEngine]preloadBackgroundMusic:@"myMusicFile"];
// or
[[SimpleAudioEngine sharedEngine]preloadEffect:@"myEffectSoundFile"];
effect is used for small files, background music for big files which often loop
easiest way...and it's awesome at memory management (aka..it releases itself when you receive level 1 memory warning)
check out the class references for more methods and details
edit:
just do something like:
if([[CCTextureCache sharedTextureCache] addImage:@"myImageName"]) {
//image loaded
}
what i forgot to say is that if you use the if you wont need to do the other one, so the if replaces the loading itself (but i guess that's obvious) and yet i said it just to be perfectly clear
Upvotes: 1