2012-10-02 59 views
1

我有,我确认我把这个方法之前不存在的文件目录下的文件:如何从文档目录加载CCSpriteBatchNode?

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

回答

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之前。

2

您可以使用CCSpriteBatchNode并通过设置searchPath,例如拉离(你想要或任何其他目录)的文件目录中的文件像这样:

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

这增加aDirectory到的Cocos2D将寻找与文件名的文件中搜索路径,你在[CCSpriteBatchNode batchNodeWithFile:@"aFile"];

指定