Setting up a plist to store application data (not settings) for an iPhone game

后端 未结 2 1966
逝去的感伤
逝去的感伤 2020-12-05 16:52

I am writing an iPhone game and have it working well with my level data hard coded but I would like to store the level data in a plist a load it at launch. I have never use

相关标签:
2条回答
  • 2020-12-05 17:17

    What you should do is create an NSDictionary with all of the applicable data that you want, and read it from and write it to NSUserDefaults.

    0 讨论(0)
  • 2020-12-05 17:22
    • Add your plist file as a resource file in your project. Say, it's name is config.plist

    • Get the path for the resource file. (Though, be careful of [NSBundle mainBundle] as it will not return the unit test bundle in a unit test.)

    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"config" ofType:@"plist"];
    
    • Your root object is an array of levels. Load it in a NSArray.
    // this is autoreleased. you can retain it if needed
    NSArray *levelArray = [NSArray arrayWithContentsOfFile:plistPath];
    
    • Now you have the array of levels. Each level is a dictionary. Load the level i (counting from 0).
    NSDictionary *level = [levelArray objectAtIndex:i];
    
    • Now you can get the objects from level dictionary by using objectForKey method. For example, to get the sequence array:
    NSArray *seq = [level objectForKey:@"levelSequence"];
    
    • You can loop through the levelArray to alloc, init and add to levels array for all your levels.

    Hope it helps. Please note that I have not compiled the code, so there might be some typos.

    0 讨论(0)
提交回复
热议问题