Reputation: 16298
I need to add downloaded spriteframe (plist/png set) from Documents folder to the spriteframe cache in Cocos2D v2.
I had this codebase working for a long time, but it doesn't work anymore and I just can't figure out right now how to add spriteframe contents that do not exist right down in the root of the bundle.
From what it looks like, I used to add the plist/png pair of spriteframes by using a relative path and send that to CCSpriteFrameCache. Something like ../Documents/hosteddownloads/somespriteframe.plist
etc. Normally you would just send myspriteframe.plist
to [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:spriteframefile];
.
I know the relative path from the bundle to the plist and to the png. If only I could make CCSpriteFrameCache load the stuff if I just specified those paths...
Upvotes: 1
Views: 66
Reputation: 9079
Hmmm ... never used a relative path, but this works for me :
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * _documentDir = [paths objectAtIndex:0];
NSString *fqn = [_documentDir stringByAppendingPathComponent:@"filename.plist"];
TRACE(@"%@", fqn);
which in this case yields in the log
-[GEFileUtil init] : /Users/yvesleborg/Library/Developer/CoreSimulator/Devices/FBF0C759-4ECB-468D-99FC-6BDA9982351A/data/Containers/Data/Application/2FF9D117-3763-4AE2-A1FA-4B25E1308CC2/Documents/filename.plist
This way you can compute the absolute path, and protect yourself from possible changes in the environment. Also, CCSpriteFrameCache will probably take into account the fact that the path is absolute and behave appropriately.
Finally, the .plist embeds the texture name, and confusion could ensue. I use another constructor to load the cache, like this:
NSString *plist = [_documentDir stringByAppendingPathComponent:@"filename.plist"];
NSString *png = [_documentDir stringByAppendingPathComponent:@"filename.png"];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:plist textureFilename:png];
Upvotes: 1