cocos2d-iphoneccspritebatchnode

How do I load a CCSpriteBatchNode from the documents directory?


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


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.