Storing Sprite Kit Level Data

社会主义新天地 提交于 2020-01-14 12:35:52

问题


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?


回答1:


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.

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.



来源:https://stackoverflow.com/questions/20501629/storing-sprite-kit-level-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!