1

在调用此方法之前,我在文档目录中有一个文件,我验证该文件确实存在:

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

4

2 回答 2

2

您可以使用 CCSpriteBatchNode 并通过设置从 Documents 目录(或您想要的任何其他目录)中提取文件searchPath,例如:

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

这将添加aDirectory到 Cocos2D 将在其中查找具有您在其中指定的名称的文件的可搜索路径[CCSpriteBatchNode batchNodeWithFile:@"aFile"];

于 2014-04-18T14:53:14.610 回答
1

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 之前检查纹理是否已存在于缓存中。

于 2012-10-02T22:34:20.163 回答