gpt4 book ai didi

ios - 如何从我的文档目录加载 TMX map ?

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:06:09 25 4
gpt4 key购买 nike

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

- (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];
}

这将下载两个 Assets 。然后我尝试加载我的地​​图:

[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 map 将 tileset 作为相对路径加载:

<?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>

如果这是从 Bundle 加载的,那就没问题了。为什么它在 Documents 目录中不起作用?

最佳答案

我将把这个作为 cocos2d 中的错误(或功能请求)归档。在尝试加载文件时,它将尝试仅从包中加载它。它不会从文档目录加载它。

所以我修好了!

在 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 目录中。如果是,请返回!

现在我的 map 加载没有问题。

关于ios - 如何从我的文档目录加载 TMX map ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22285471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com