Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.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
Objective c 将坐标从文本文件加载到NSMutableArray中_Objective C_Xcode_Ios_Cocos2d Iphone_Box2d - Fatal编程技术网

Objective c 将坐标从文本文件加载到NSMutableArray中

Objective c 将坐标从文本文件加载到NSMutableArray中,objective-c,xcode,ios,cocos2d-iphone,box2d,Objective C,Xcode,Ios,Cocos2d Iphone,Box2d,我正在从物理编辑器生成一个普通的txt文件。它包含多边形的所有顶点。因为我想对多边形进行纹理处理,所以我通过位于以下位置的三角剖分方法运行它: 我需要我的数据从NSMutableArray到doso,这样它才能工作 Physics editor可以导出.plist和.txt文件,但为了简单起见,我只想从.txt文件中获取顶点,并将它们转换为CGPoints,然后将它们添加到NSMutableArray中 txt文件如下所示: (53.40111993408203,-44.401119934082

我正在从物理编辑器生成一个普通的txt文件。它包含多边形的所有顶点。因为我想对多边形进行纹理处理,所以我通过位于以下位置的三角剖分方法运行它:
我需要我的数据从NSMutableArray到doso,这样它才能工作

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

txt文件如下所示:

(53.40111993408203,-44.40111993408203) , (74.4011993408203, -38.4011993408203) , (-0.598802387714386, 0.598802387714386) , (-0.598802387714386, -39.4011993408203) , ...

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

然后,可以将此方法用于不同的文本文件以创建简单性。等: Level1ground.txt、Level2ground.txt。。如果我能让它运行,那就太棒了

有人能帮我吗?
非常感谢,

Oliver.

在这种情况下,您可能需要使用
NSScanner
实例。请参见-
scanupCharactersSet:
-scanFloat。

此解决方案假定您已将文件放入字符串中。 您可以使用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);
}

这很有效!,除了一个括号是向后的。非常感谢。抱歉,解析器不是万无一失的,只是为了给您指出正确的方向。在示例应用程序中为我工作。