Monkeyanator
Monkeyanator

Reputation: 1416

Storing Sprite Kit Level Data

There is a project I am working on in which a set of targets appear onto the screen. The targets should appear a certain amount at the same time, and with a certain delay in between each appearance. My question is how I would correctly store the level data for this game. I was considering using a csv file to store the level data (target type, location, delay, etc.), but I'm wondering if there is a better way to do it. I had also considered making a level object to store level information, but I'm not sure. Apple says to use 'Archives of sprite nodes', but I can't seem to find out what that means. Any thoughts?

Upvotes: 4

Views: 1801

Answers (1)

DrummerB
DrummerB

Reputation: 40211

Did you think about using a plist file instead? That would be the easiest to parse. You could have an array of targets (dictionarys) and then define position, absolute delay and what ever else you want.

enter image description here

Then just read it into an array:

NSArray *targets = [NSArray arrayWithContentsOfFile:plistPath];
for (NSDictionary *dictionary in targets) {
    CGPoint position = CGPointMake([dictionary[@"positionX"] floatValue], 
                                   [dictionary[@"positionY"] floatValue]);
    float delay = [dictionary[@"time"] floatValue];
    // Do something with this information, maybe create a Target instance etc.
}

You could also do the same with CSV files, but they would be a little bit more difficult to parse (not too difficult though).

Regarding archives, what Apple means is that all Sprite Kit classes support NSCoding. That means they can be archived into a file (or NSData object) and later unarchived from that archive. This is however different from what you want to do. Archiving would create a single "snapshot" of the current state of the game. So this would be nice to save the game for instance when the user leaves.

Upvotes: 5

Related Questions