首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从文档目录加载TMX?

如何从文档目录加载TMX?
EN

Stack Overflow用户
提问于 2014-03-09 17:27:21
回答 1查看 238关注 0票数 2

我正在从服务器下载TMX地图和Tileset,并将它们保存到iOS应用程序文档目录中:

代码语言:javascript
复制
- (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];
}

这将下载这两种资产。然后,我尝试加载我的Map:

代码语言:javascript
复制
[self downloadMap:^(NSURL *filePath) {
    self.map = [CCTMXTiledMap tiledMapWithTMXFile:[filePath absoluteString]];
}];

而Cocos2D拒绝加载它。它找不到文件,尽管日志声明:

代码语言:javascript
复制
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

如果我枚举文档目录中的所有文件,就会得到:

代码语言:javascript
复制
Files: (
    "test_map.tmx",
    "tmw_desert_spacing.png"
)

我已经说服自己那些文件在那里。TMX映射将tileset加载为一个相对路径:

代码语言:javascript
复制
<?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>

如果这是从捆绑装载,它将是好的。为什么它不能从文档目录中工作呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-09 22:31:23

我将把这个错误作为cocos2d中的一个bug (或者一个特性请求)归档。当试图加载一个文件时,它将尝试只从包加载它。它很简单,不会从文档目录加载它。

所以我修好了!

在CCFileUtils.m中:

代码语言:javascript
复制
- (NSString *)applicationDocumentsDirectory;
{
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
}

这是获取iOS应用程序目录的一种方便方法。

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

代码语言:javascript
复制
-(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目录之前,检查资源是否存在于directory中。如果是的话,把它还回去!

现在我的地图没有问题。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22285471

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档