Question

I have a file in the documents directory that I verify does exist before I call this method:

CCSpriteBatchNode *batchNode = [[CCSpriteBatchNode alloc] initWithFile:filePath capacity:9];

The error reported is:

-[CCFileUtils fullPathFromRelativePath:resolutionType:]  : cocos2d: Warning: File not found: decompressed_file.png

cocos2d: CCTexture3d: Can't create Texture.  cgImage is nil;

Using Cocos2d 2.0

Was it helpful?

Solution

The cocos2d functions file methods assume you are loading from your bundle resources. I do not think you can load the file directly but what you can do is load it into a UIImage and then create a CCTexture2D with a CGImage. With your CCTexture2D you can create a CCSpriteBatchNode.

UIImage *img = [UIImage imageWithContentsOfFile:filePath];
CCTexture2d *tex = [[CCTextureCache sharedTextureCache] addCGImage:[img CGImage] forKey:@"myTexture"];
CCSpriteBatchNode *batch = [CCSpriteBatchNode batchNodeWithTexture:tex capacity:9];

These steps are whats going on in the background anyway when you create a CCSpriteBatchNode using the normal methods. Except for the bundle assumption.

If you are making the sprite batch node more than once for some reason, you can check if the texture already exists in the cache before reloading the UIImage.

OTHER TIPS

You can use CCSpriteBatchNode and pull files from the Documents directory (or any other directory you want) by setting the searchPath, e.g. like so:

NSString *documentDirectory = aDirectory;
NSMutableArray * loadSearchPath = [[CCFileUtils sharedFileUtils].searchPath mutableCopy];
[loadSearchPath addObject:documentDirectory];
[CCFileUtils sharedFileUtils].searchPath = [loadSearchPath copy];

This adds aDirectory to the searchable paths where Cocos2D will be looking for the file with the name you specify in [CCSpriteBatchNode batchNodeWithFile:@"aFile"];

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top