Hi I thought I would preload some images, so that there would not be a stutter when a user enters the mode in my application in which Cocos2D lives.
So I created a plist file called, "Cocos2DTextures.plist", which I parse in some function after the usual Cocos2D setup.
That is of course:
if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )
[CCDirector setDirectorType:kCCDirectorTypeThreadMainLoop];
CCDirector *director = [CCDirector sharedDirector];
[director setDeviceOrientation:kCCDeviceOrientationPortrait];
[director setDisplayFPS:NO];
[director setAnimationInterval:1.0/60];
// Enable retina display
if( [UIScreen instancesRespondToSelector:@selector(scale)] )
[director setContentScaleFactor: [[UIScreen mainScreen] scale]];
This is the function which parses the plist file, and tells CCTextureCache to load the images.
-(void) preloadTexturesUsingPropertyListNamed:(NSString*)propertyListFileName
{
NSString *propertyListFileNameWithExtension = [propertyListFileName stringByAppendingPathExtension:@".plist"];
// Make sure it's created
NSString *errorDescription = nil;
NSPropertyListFormat format;
NSString *rootPath = [NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *propertyListFilePath = [rootPath stringByAppendingPathComponent: propertyListFileNameWithExtension];
// See if it's in the documents directory, if not grab it from the mainbundle
if( ![[NSFileManager defaultManager] fileExistsAtPath: propertyListFilePath]) {
propertyListFilePath = [[NSBundle mainBundle] pathForResource:propertyListFileName ofType:@"plist"];
}
NSData *propertyListXML = [[NSFileManager defaultManager] contentsAtPath:propertyListFilePath];
NSDictionary *propertyListDictionary = (NSDictionary*) [NSPropertyListSerialization propertyListFromData:propertyListXML
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:&format
errorDescription:&errorDescription];
// Now that we have the plist dictionary - lets preload the objects
for (NSString *textureFileName in propertyListDictionary)
{
// Strip the filename into 2 parts, filename and extension
NSString *textureFileNameWithoutExtension = [textureFileName stringByDeletingPathExtension];
NSString *textureFileNameExtension = [textureFileName pathExtension];
NSString *finalTextureName = [textureFileNameWithoutExtension copy];
// Preload the HD version instead - if we are in a retina display (device >= iphone 4G and iOS >= 4.0)
if( [UIScreen instancesRespondToSelector:@selector(scale)] )
{
// Are we in HD?
if([[UIScreen mainScreen] scale] > 1.0)
{
// Is there an HD version of that texture?
NSString *finalTexturePathHD = [[NSBundle mainBundle] pathForResource:[finalTextureName stringByAppendingString:@"-hd"] ofType:textureFileNameExtension];
// HD version exist, lets use that
if(finalTexturePathHD)
finalTextureName = [finalTextureName stringByAppendingString:@"-hd"];
}
}
// Load the texture
NSLog(@"Asking to preload texture: %@", [finalTextureName stringByAppendingPathExtension:textureFileNameExtension]);
// Texture exist load it
[[CCTextureCache sharedTextureCache] addImageAsync:[finalTextureName stringByAppendingPathExtension:textureFileNameExtension]
target:self
selector:@selector(imagePreloadSuccessful:)];
}
}
The problem is that, when I go into the mode which contains Cocos2D, all of my textures or some, did not load at all.
I do all of that on a user interaction, so it can be 15 seconds or 2,3,4,5 minutes later - does not matter same result.
So when I call:
[[CCSprite spriteWithFile: @"someFileName"]'
which I assume should check the cache for the texture and use that. The objects come up blank, there is no run-time error, the texture is just white.
When I comment out
[[CCTextureCache sharedTextureCache]
addImageAsync:[finalTextureName stringByAppendingPathExtension:textureFileNameExtension]
target:self
selector:@selector(imagePreloadSuccessful:)];
Everything works fine again.
Does anyone have any ideas what is going on, is this incorrect usage of the CCTextureCache?