I think it's a bug.... I'm running with the latest SVN release, and can get it to happen.... to fix it, I created a category for TextureAtlas.initWithFile, which for me was accessing a singleton, but for anyone else that wants to do this, could call whatever.
@implementation TextureAtlas (filePathResolver)
-(id) initWithFile:(NSString*)file capacity:(NSUInteger)n
{
// My line of code here was different than this, but this is where you
// could easily change where the file path comes from, if you like.
NSString *fullFilePath = [FileUtils fullPathFromRelativePath:file];
Texture2D *tex = [[TextureMgr sharedTextureMgr] addImage:fullFilePath];
return [self initWithTexture:tex capacity:n];
}
@end
I put a sample together that duplicates the problem, and posted it as an issue:
http://code.google.com/p/cocos2d-iphone/issues/detail?id=539
This whole thing got me thinking that it'd be nice to be able to globally intercept all resource requests somehow, and it'd be easy to do if all resource requests went through a stub method in say, FileUtils. Then the stub method could either fire off a selector for resolving files if a delegate existed, or could be easily overridden by a category.
e.g. FileUtils could include the following method that would be used by all "initWithFile", etc type requests:
@implementation FileUtils
+(NSString*) resolveResourceName:(NSString*) resourceName
{
// This method exists to be overridden using a category.
return resourceName;
}
If someone wanted to override how a resource was retrieved, they'd simply add a category like so:
@implementation FileUtils (resolveFilePaths)
+(NSString*) resolveResourceName:(NSString*) resourceName
{
// Do something different here than the default. e.g. Prepend the users docs directory if required.
return [[MyGameManager getManager] getUsersDocumentsDirectoryFilename:resourceName];
}
The advantage of the category approach would be that by default, no extra lookups would be done. If a delegate was used, then there'd be a check to see if a delegate was assigned, and then a check for the resolveFilePath method.
Is there a better way to do this type of thing?