2

我正在尝试使用基本的父子对象继承系统编写一个简单的 RPG,该继承系统被保存并加载到游戏状态中,但是当加载发生时,父级没有数据出现,我很难理解为什么。

为简单起见,我做了一个简单的父子对象继承,其中,

Player从父母继承Character

我把所有东西都放在一个数组(listOfPlayers)中,然后归档对象;当我来加载数据时,Character对象不再存在。

我已经登录到控制台以下;

METHOD: saveGameState()
2012-07-17 20:12:24.762 TestState[72342:40b] self.listOfPlayers (1)
2012-07-17 20:12:24.762 TestState[72342:40b] p.name = BOB
2012-07-17 20:12:24.763 TestState[72342:40b] p.xp = 1000

METHOD: loadGameState()
2012-07-17 20:12:27.098 TestState[72342:40b] Last Save Date = 2012-07-17 19:12:24 +0000
2012-07-17 20:12:27.099 TestState[72342:40b] self.listOfPlayers (1)
2012-07-17 20:12:27.100 TestState[72342:40b] p.name = (null)
2012-07-17 20:12:27.101 TestState[72342:40b] p.xp = 1000
2012-07-17 20:12:27.102 TestState[72342:40b] State:Loaded

保存状态似乎正在保存名称(Character模型的一部分),但随后加载状态的名称似乎为null

我不确定我做错了什么。

我的代码现在如下;

// Inside my app delegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Singleton
    GameStateManager *state = [GameStateManager sharedGameStateManager];
    [state setupGameState];
    return YES;
}

// Character.h
@interface Character : NSObject
<NSCoding>
{
    NSString *name;
}

@property (nonatomic, retain) NSString *name;

@end

// Character.m
@implementation Character
@synthesize name;

#pragma mark - NSCoding

-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    if (self) 
    {
        NSLog(@"Loading character object");
        self.name   = [aDecoder decodeObjectForKey:@"name"];
    }

    return self;
}

-(void)encodeWithCoder:(NSCoder *)aCoder
{      
    // Archive the object
    NSLog(@"Archiving character object");

    [aCoder encodeObject:self.name forKey:@"name"];
}
@end


// Player.h
#import "Character.h"

@interface Player : Character
<NSCoding>
{
    NSNumber *xp; // xp points
}
@property (nonatomic, retain) NSNumber *xp;

+(NSMutableArray *) createPlayers;
@end

// Player.m
@implementation Player
@synthesize xp;

- (id)init {
    self = [super init];
    if (self) {
    }
    return self;
}

+(NSMutableArray *) createPlayers
{
    NSMutableArray *list = [NSMutableArray array];

    Player *p1 = [[Player alloc] init];
    [p1 setXp:[NSNumber  numberWithInt:1000]];
    [p1 setName:@"BOB"];
    [list addObject:p1];
    [p1 release];

    return list;
}


#pragma mark - NSCoding

-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    if (self) 
    {
        self.xp = [aDecoder decodeObjectForKey:@"xp"];
    }

    return self;
}

-(void)encodeWithCoder:(NSCoder *)aCoder
{      
    // Archive the object
    [aCoder encodeObject:self.xp forKey:@"xp"];
}
@end

// GameStateManager.m file    
-(void)saveGameState
{
    NSLog(@"METHOD: saveGameState()");

    self.lastSaveDate   = [NSDate date];

    NSLog(@"self.listOfPlayers (%d)", [self.listOfPlayers count]);


    for (Player *p in self.listOfPlayers) {
        NSLog(@"p.name = %@", p.name);
        NSLog(@"p.xp = %d", [p.xp intValue]);
    }

    NSString *file = kGameSaveFile;    
    NSString *documentsDirectory = [MyHelpers getDocumentsDirectory];
    NSString *gameStatePath = [documentsDirectory stringByAppendingPathComponent:file];
    BOOL result;

    NSLog(@"gameStatePath = %@", gameStatePath);

    // Set up the encoder and storage for the game state data
    NSMutableData *gameData;
    NSKeyedArchiver *encoder;
    gameData = [NSMutableData data];
    encoder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:gameData];

    // Archive the object
    [encoder encodeObject:self.lastSaveDate forKey:@"lastSaveDate"];
    [encoder encodeObject:self.listOfPlayers forKey:@"listOfPlayers"];

    // Finish encoding and write to disk
    [encoder finishEncoding];
    result = [gameData writeToFile:gameStatePath atomically:YES];
    [encoder release];

    if (result == YES)
    {
        NSLog(@"Result = OK");
    } else {
        NSLog(@"Result = BAD");
    }
}

-(void)loadGameState
{
    NSLog(@"METHOD: loadGameState()");

    NSString *file = kGameSaveFile;   
    NSString *documentsDirectory = [MyHelpers getDocumentsDirectory];
    NSString *gameStatePath = [documentsDirectory stringByAppendingPathComponent:file];


    NSMutableData *gameData;
    NSKeyedUnarchiver *decoder;    
    gameData = [NSData dataWithContentsOfFile:gameStatePath];

    // Check to see if the .dat file exists, and load contents
    if (gameData)
    {
        decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:gameData];

        self.lastSaveDate = [[decoder decodeObjectForKey:@"lastSaveDate"] retain];
        self.listOfPlayers = [[decoder decodeObjectForKey:@"listOfPlayers"] retain]; 

        [decoder finishDecoding];
        [decoder release];

        NSLog(@"Last Save Date = %@", self.lastSaveDate);
        NSLog(@"self.listOfPlayers (%d)", [self.listOfPlayers count]);


        for (Player *p in self.listOfPlayers) {
            NSLog(@"p.name = %@", p.name);
            NSLog(@"p.xp = %d", [p.xp intValue]);
        }

    } else {

        NSLog(@"No game data!");
        assert(-1);
        abort();

    }


    NSLog(@"State:Loaded");
}

编辑,

我认为它必须有if ( (self = [super initWithCoder:aDecoder]) )并且[super encodeWithCoder:aCoder];

4

1 回答 1

3

使用

 if ( (self = [super initWithCoder:aDecoder]) ) and [super encodeWithCoder:aCoder];

帮助我解决了这个问题,这个问题也有帮助的答案。

谢谢

于 2012-07-24T08:14:58.247 回答