Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何从文档目录加载TMX映射?_Ios_Cocos2d Iphone_Tmx_Tiled - Fatal编程技术网

Ios 如何从文档目录加载TMX映射?

Ios 如何从文档目录加载TMX映射?,ios,cocos2d-iphone,tmx,tiled,Ios,Cocos2d Iphone,Tmx,Tiled,我正在从我的服务器下载TMX地图和Tileset,并将它们保存到iOS apps documents目录中: - (void)downloadMap:(void (^)(NSURL *filePath))callback; { NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; AFURLSessionManager *manager

我正在从我的服务器下载TMX地图和Tileset,并将它们保存到iOS apps documents目录中:

- (void)downloadMap:(void (^)(NSURL *filePath))callback;
{
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

    NSURL *URL = [NSURL URLWithString:@"http://localhost:9950/download"];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];

    NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
        NSURL *documentsDirectoryPath = [NSURL fileURLWithPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]];
        return [documentsDirectoryPath URLByAppendingPathComponent:[response suggestedFilename]];
    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
        DLog(@"Saved: %@", filePath);
        [self downloadTileMap:^(NSURL *filePath) {
            if (callback) {
                callback(filePath);
            }
        }];
    }];
    [downloadTask resume];
}
这将下载这两种资产。然后我尝试加载我的地图:

[self downloadMap:^(NSURL *filePath) {
    self.map = [CCTMXTiledMap tiledMapWithTMXFile:[filePath absoluteString]];
}];
Cocos2D拒绝加载它。即使日志状态为:

Saved: file:///Users/ethan/Library/Application%20Support/iPhone%20Simulator/7.1/Applications/2FDEA5E2-052D-4E7B-B7DD-3FA29B5BD4D0/Documents/test_map.tmx
Saved: file:///Users/ethan/Library/Application%20Support/iPhone%20Simulator/7.1/Applications/2FDEA5E2-052D-4E7B-B7DD-3FA29B5BD4D0/Documents/tmw_desert_spacing.png
-[CCFileUtils fullPathForFilename:resolutionType:] : cocos2d: Warning: File not found: file:///Users/ethan/Library/Application%20Support/iPhone%20Simulator/7.1/Applications/2FDEA5E2-052D-4E7B-B7DD-3FA29B5BD4D0/Documents/tmw_desert_spacing.png
如果我枚举Documents目录中的所有文件,我会得到:

Files: (
    "test_map.tmx",
    "tmw_desert_spacing.png"
)
我确信文件就在那里。TMX贴图将瓷砖集作为相对路径加载:

<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" width="16" height="40" tilewidth="32" tileheight="32">
 <tileset firstgid="1" name="tmw_desert_spacing" tilewidth="32" tileheight="32" spacing="1" margin="1">
  <image source="tmw_desert_spacing.png" width="265" height="199"/>
 </tileset>
 <layer name="Walkable" width="16" height="40">
  <data encoding="base64" compression="gzip">
   H4sIAAAAAAAAA+3DAQ0AAAzDoCqZf5kXckhYNVVV3zxRXBimAAoAAA==
  </data>
 </layer>
 <layer name="Collidable" width="16" height="40">
  <data encoding="base64" compression="gzip">
   H4sIAAAAAAAAA82TwQ5AMBBEV51wwgkHixP+//9cG9FNR2viJXMyk0llVgRDQT8TJXYwumIYA5o8z2D41oA2L78YviOg08vvhi83msnDwgEqH/I1oOYh3wHqE9+air789kXfFxSC7cHd8pVge6hv+VawPXQ5Hi28/8zqQYi5dWsXMbdu7SLm1q1d/AUl9cykHhYXallnFQAKAAA=
  </data>
 </layer>
</map>

H4SIAAAAAAAA+3DAQ0AAAZDOCQZF5KHYNVV3ZXRXBIMAOAOAAA==
H4SIAAAAAA82TWQ5AMBBEV51WWGKHIXP+//9cG9FNR2viJXMyk0llVgRDQT8TJXYwumIYA5o8z2D41oA2L78YviOg08vvhi83msnDwgEqH/I1oOYh3wHqE9+AIR789KXFxSC7CHD8PVGE6HV+VawPXQ5Hi28/8ZQYI5DWSXMB7SLM1Q1D/Aul9CykhyxallNFQAA=

如果这是从捆绑中加载的,就可以了。为什么它不能从Documents目录中工作?

我将把它作为一个bug归档到cocos2d中(或者作为一个功能请求)。在尝试加载文件时,它将尝试仅从捆绑包加载该文件。它不会从Documents目录加载它

所以我把它修好了

在CCFileUtils.m中:

- (NSString *)applicationDocumentsDirectory;
{
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
}
这是获取iOS应用程序目录的方便方法

然后我将
pathForResource:ofType:inDirectory:
更改为:

-(NSString*) pathForResource:(NSString*)resource ofType:(NSString *)ext inDirectory:(NSString *)subpath
{
    // An absolute path could be used if the searchPath contains absolute paths
    if( [subpath isAbsolutePath] ) {
        NSString *fullpath = [subpath stringByAppendingPathComponent:resource];
        if( ext )
            fullpath = [fullpath stringByAppendingPathExtension:ext];

        if( [_fileManager fileExistsAtPath:fullpath] )
            return fullpath;
        return nil;
    }

    if (resource) {
        NSString *fullPath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:resource];
        if( ext )
            fullPath = [fullPath stringByAppendingPathExtension:ext];

        if ([_fileManager fileExistsAtPath:fullPath]) {
            return fullPath;
        }
    }

    // Default to normal resource directory
    return [_bundle pathForResource:resource
                             ofType:ext
                        inDirectory:subpath];
}
基本上,在默认进入Bundle目录之前,检查Documents目录中是否存在资源。如果有,请退回


现在,我的地图加载没有问题。

当您尝试加载tmx时,请进入ccfileutils。这很麻烦,但是ccfileutils做了一些奇怪的事情,所以如果你发现它到底出了什么问题,应该可以帮助你理解这个问题。甚至可能是一个误导性的警告,告诉你它找不到-hd tileset图像。