2012-11-19 39 views
0

我的应用程序从URL获取一些资源,特别是.pvr.ccz,并将它们保存在Documents目录中。如何让Cocos2D可以搜索特定路径中的资源?

NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
... 
[responseObject writeToFile:[documentsDirectoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", <file_name>, @"pvr.ccz"]] options:NSAtomicWrite error:nil]; 

之后创建使用.plist.pvr.ccz一个精灵。已在项目中的属性列表中指定的.png图像列表。

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"<property_list_name>.plist"]; 
CCSprite *sprite = [CCSprite spriteWithSpriteFrameName:[NSString stringWithFormat:@"<file_name>.png"]]; 

这将被警告,-[CCFileUtils fullPathFromRelativePath:resolutionType:] : cocos2d: Warning: File not found: <file_name>.pvr.ccz

有没有什么办法让Cocos2D中可以从保存在特定文件夹中的网址搜索资源?或者有什么方法可以在Cocos2D可以找到它们的地方保存来自URL的资源?

回答

1

我相信如果你指定一个完整路径到coco的话,它会看起来那里。因此

NSString*fqn = [documentsDirectoryPath [email protected]"<property_list_name>.plist"]; 
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:fqn]; 

还没有试过这个,但它应该工作。

编辑:假定.plist在项目中,但.pvr.ccz位于文档字典中。进一步假设你的纹理被称为“下载-hd.pvr.ccz”,该项目使HD的设备,下面的例子应该找到您的文件:

NSString*[email protected]"downloaded-hd.pvr.ccz"; 
NSString*fqn = 
    [documentsDirectoryPath stringByAppendingPathComponent:textureFileName]; 
[[CCSpriteFrameCache sharedSpriteFrameCache] 
    addSpriteFramesWithFile:@"<property_list_name>.plist" 
      textureFileName:fqn]; 

我认为-HD将正确地默默处理。

+0

未找到的文件不是直接调用的文件,即'.pvr.ccz',当'CCSprite * sprite = [CCSprite spriteWithSpriteFrameName:[NSString stringWithFormat:@“ .png”]];'被调用时,Cocos2D将通过'.plist'中指定的坐标从'.pvr.ccz'中获得该图像。 – Protocole

+0

我刚刚发现我的解决方案,它将'.plist'和'.pvr.ccz'保存在同一个地方。你的解决方案也是工作而且非常相似。非常感谢你。 – Protocole