objective-c - 将文本文件中的坐标加载到 NSMutableArray 中

标签 objective-c xcode ios cocos2d-iphone box2d

我正在从物理编辑器生成一个纯 txt 文件。它包含我的多边形的所有顶点。因为我希望我的多边形具有纹理,所以我通过位于: https://github.com/asinesio/cocos2d-PRKit/ 的三角测量方法运行它。
我需要来自 NSMutableArray 的数据才能使其正常工作。

物理编辑器可以导出.plist和.txt文件,但为了简单起见,我只想从.txt文件中获取顶点并将它们转换为CGPoints,然后将它们添加到NSMutableArray中

txt 文件如下所示:

(53.4011993408203, -44.4011993408203) , (74.4011993408203, -38.4011993408203) , (-0.598802387714386, 0.598802387714386) , (-0.598802387714386, -39.4011993408203) , ...

我认为方法是:
从源加载数据。
扫描不包括括号和字母字符的数据。
将逗号之前的所有数据添加到 CGPoint (x(1),y(0)) 中。
然后扫描所有数据直到下一个逗号并将其插入到 CGPoint (x(1),y(1)) 中。
然后将此 CGPoint 添加到 NSMutableArray 中。 继续扫描文档,直到添加所有坐标。

此方法可以与不同的文本文件一起使用以简化操作。 ETC: Level1ground.txt、Level2ground.txt..如果我能让它运行那就太好了。

有人可以帮我解决这个问题吗?
非常感谢,
奥利弗。

最佳答案

此解决方案假设您已将文件加载到字符串中。 您可以使用 NSBundle 类的 pathForResource:ofType:inDirectory: 来加载文件。

NSArray * rawPoints = [@"(53.4011993408203, -44.4011993408203) , (74.4011993408203, -38.4011993408203) , (-0.598802387714386, 0.598802387714386) , (-0.598802387714386, -39.4011993408203)" componentsSeparatedByString:@" , "];
for (NSString * rawPoint in rawPoints) {
    NSString *tmp = [rawPoint stringByReplacingOccurrencesOfString:@"(" withString:@""];
    tmp = [tmp stringByReplacingOccurrencesOfString:@"(" withString:@""];

    NSArray * coordinates = [tmp componentsSeparatedByString:@", "];

    CGPoint point;
    for (NSString * coordinate in coordinates) {
        point = CGPointMake([[coordinates objectAtIndex:0] floatValue],
                            [[coordinates objectAtIndex:1] floatValue]);
    }
    NSLog(@"x:%f, y:%f", point.x, point.y);
}

关于objective-c - 将文本文件中的坐标加载到 NSMutableArray 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5578743/

相关文章:

objective-c - Qt5 似乎找不到 Objective-C 库 : unknown type name 'NSAutoreleasePool' , 等

ios - 如何获取 CFBundleShortVersionString 作为常量

ios - 如何在握手期间使用 NSStream 生成证书异常

ios - 在 segue 上移动图像以加载到另一个 UIImageView

ios - 嵌套的 JSON 访问/为 UITextField 赋值

ios - 使用默认选项以外的语言进行本地化,例如瑞典语

objective-c - CCLabelTTF SetString 未更新

objective-c - 使用openssl编译报错

ios - 需要将库与 A​​dMob 和 Firebase 关联起来吗?

ios - iOS 中 UIDatePicker 的默认日期