在调用此方法之前,我验证了documents目录中是否存在一个文件:
CCSpriteBatchNode *batchNode = [[CCSpriteBatchNode alloc] initWithFile:filePath capacity:9];报告的错误为:
-[CCFileUtils fullPathFromRelativePath:resolutionType:] : cocos2d: Warning: File not found: decompressed_file.png
cocos2d: CCTexture3d: Can't create Texture. cgImage is nil;使用Cocos2d 2.0
发布于 2012-10-03 06:34:20
cocos2d函数文件方法假定您是从捆绑包资源加载的。我不认为您可以直接加载该文件,但您可以做的是将其加载到UIImage中,然后使用CGImage创建一个CCTexture2D。您可以使用CCTexture2D创建一个CCSpriteBatchNode。
UIImage *img = [UIImage imageWithContentsOfFile:filePath];
CCTexture2d *tex = [[CCTextureCache sharedTextureCache] addCGImage:[img CGImage] forKey:@"myTexture"];
CCSpriteBatchNode *batch = [CCSpriteBatchNode batchNodeWithTexture:tex capacity:9];这些步骤是使用普通方法创建CCSpriteBatchNode时在后台发生的事情。除了捆绑假设。
如果由于某种原因多次创建精灵批处理节点,可以在重新加载UIImage之前检查该纹理是否已存在于缓存中。
发布于 2014-04-18 22:53:15
通过设置searchPath,您可以使用CCSpriteBatchNode并从Documents目录(或您想要的任何其他目录)中拉出文件,例如:
NSString *documentDirectory = aDirectory;
NSMutableArray * loadSearchPath = [[CCFileUtils sharedFileUtils].searchPath mutableCopy];
[loadSearchPath addObject:documentDirectory];
[CCFileUtils sharedFileUtils].searchPath = [loadSearchPath copy];这会将aDirectory添加到Cocos2D将在其中查找具有您在[CCSpriteBatchNode batchNodeWithFile:@"aFile"];中指定的名称的文件的可搜索路径
https://stackoverflow.com/questions/12697402
复制相似问题