2012-10-02 58 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"];

指定