设置plist来存储iPhone游戏的应用程序数据(而不是设置) [英] Setting up a plist to store application data (not settings) for an iPhone game

查看:91
本文介绍了设置plist来存储iPhone游戏的应用程序数据(而不是设置)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一款iPhone游戏,并且我的硬件编码水平数据能够很好地运行,但是我希望将水平数据存储在plist中并在启动时加载它。我从来没有使用过plists而且我很难理解如何根据我的数据模型设置plist。

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:

(在我的appDelegate中)

(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//
}

(这就是我实现Level Level的方法。)

(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

我只是没有得到如何设置plist来存储我的数据以匹配我的模型。请问有人可以给我一些建议吗?

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?

附加:
(这是我认为我需要设置plist的方式 - 数据模型只是三个变量初始化我的等级(上图)。如果你查看我当前的plist,可能会更清楚它是如何设置的,但是每个等级包括:一个等级数,一个等级速度,以及一个表示所需顺序的数字数组。等级。)

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.)

现在,如果我有正确的设置,如何将值加载到我的程序中?

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

推荐答案


  • 将plist文件添加为项目中的资源文件。比如说,它的名字是config.plist

    • Add your plist file as a resource file in your project. Say, it's name is config.plist

      获取资源文件的路径。 (但是,请注意[NSBundle mainBundle],因为它不会在单元测试中返回单元测试包。)

      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"];
      




      • 您的根对象是一个级别数组。将其加载到NSArray中。

      • 
        // this is autoreleased. you can retain it if needed
        NSArray *levelArray = [NSArray arrayWithContentsOfFile:plistPath];
        




        • 现在你有了一系列关卡。每个级别都是字典。加载级别i(从0开始计算)。

        • 
          NSDictionary *level = [levelArray objectAtIndex:i];
          




          • 现在,您可以使用objectForKey方法从级别字典中获取对象。例如,要获取序列数组:

          • 
            NSArray *seq = [level objectForKey:@"levelSequence"];
            




            • 您可以循环使用levelArray为您的所有级别分配,初始化和添加到levels数组。 / li>

              • 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.

                这篇关于设置plist来存储iPhone游戏的应用程序数据(而不是设置)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆