Add spriteframe from documents folder in Cocos2D-v2

江枫思渺然 提交于 2019-12-24 14:10:11

问题


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...


回答1:


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];


来源:https://stackoverflow.com/questions/33098574/add-spriteframe-from-documents-folder-in-cocos2d-v2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!