开发者

Load coordinates from text-file into NSMutableArray

Im generating a plain txt file from Physics editor. It contains all t开发者_Python百科he vertices for my polygon. Because I want my polygon to be textured, im running it through a triangulation method located at: https://github.com/asinesio/cocos2d-PRKit/

And I need my data to come from an NSMutableArray to doso for it to work.

Physics editor can export .plist and .txt files, but for simplicity sake, I just want to get the vertices from the .txt file and turn them into CGPoints and then add them into a NSMutableArray

The txt file looks like this:

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

I think the method would be to:

Load the data from its source.

Scan the data excluding parenthesis and alphbetical characters.

Take all the data upto the comma and add it into the CGPoint (x(1),y(0)).

Then scan all the data upto the next comma and insert it into the CGPoint (x(1),y(1)).

Then add this CGPoint to a NSMutableArray. Continue scanning the document until all coordinates have been added.

This method could then be used with different text-files to create simplicity. Etc: Level1ground.txt, Level2ground.txt.. It would be fantastic if I could get it running.

Could someone please help me with this?

Much Appreciated,

Oliver.


This solution assumes that you have loded the file already into a string. You can make use of pathForResource:ofType:inDirectory: of NSBundle class to load a file.

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);
}


In this case, you probably want to use an NSScanner instance. See -scanUpToCharactersInSet: and -scanFloat.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜