Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

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

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 used plists and am having trouble understanding how I should set the plist up based on my data model.

Here is how I have it hard coded now:

(in my appDelegate)

- (void)loadLevels {
    //setup NSNumber objects to load into sequences
    NSNumber * rID = [[[NSNumber alloc] initWithInt:0] autorelease];
    NSNumber * bID = [[[NSNumber alloc] initWithInt:1] autorelease];
    NSNumber * gID = [[[NSNumber alloc] initWithInt:2] autorelease];
    NSNumber * yID = [[[NSNumber alloc] initWithInt:3] autorelease];
    NSNumber * rbID = [[[NSNumber alloc] initWithInt:4] autorelease];
    NSNumber * rgID = [[[NSNumber alloc] initWithInt:5] autorelease];
    NSNumber * ryID = [[[NSNumber alloc] initWithInt:6] autorelease];
    NSNumber * bgID = [[[NSNumber alloc] initWithInt:7] autorelease];
    NSNumber * byID = [[[NSNumber alloc] initWithInt:8] autorelease];
    NSNumber * gyID = [[[NSNumber alloc] initWithInt:9] autorelease];

    //Level One's Sequence
    NSMutableArray * aSequence = [[[NSMutableArray alloc] initWithCapacity:1] autorelease];

    [aSequence addObject: rID];
    [aSequence addObject: bID];
    [aSequence addObject: gID];
    [aSequence addObject: yID];
    [aSequence addObject: rbID];
    [aSequence addObject: rgID];
    [aSequence addObject: ryID];
    [aSequence addObject: bgID];
    [aSequence addObject: byID];
    [aSequence addObject: gyID];

    // Load level One
    _levels = [[[NSMutableArray alloc] init] autorelease];

    Level *level1 = [[[Level alloc] initWithLevelNum:1 levelSpeed:1.0 levelSequence:aSequence] autorelease];

    [_levels addObject:level1];
 //do the same thing for subsequent levels//
}

(this is how I have my Level Class implemented)

#import "Level.h"

@implementation Level

@synthesize levelNum = _levelNum;
@synthesize levelSpeed = _levelSpeed;
@synthesize levelSequence = _levelSequence;

- (id)initWithLevelNum:(int)levelNum levelSpeed:(float)levelSpeed levelSequence:(NSMutableArray *)levelSequence {
    if ((self = [super init])) {
        self.levelNum = levelNum;
        self.levelSpeed = levelSpeed;
        self.levelSequence = [[[NSMutableArray alloc] initWithArray:levelSequence] autorelease];
    }
    return self;
}

- (void)dealloc {
    [_levelSequence release];
    _levelSequence = nil;
    [super dealloc];
}

@end

I'm just not getting how to set up a plist to store my data to match my model. Can anyone give me some advise please?

ADDITION: (here is how I think I need to setup the plist - the data model is simply the three variables initializing my level (above). If you look at my current plist it might be more clear how it is setup, but each level is comprised of: a level number, a level speed, and an array of numbers denoting the required sequence for that level.)

My current plist setup

Now, if I do have this setup correctly how do I load the values into my program?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  • 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...