存档&&解档游戏状态

本文介绍了iOS应用中游戏存档和加载的实现方法。通过使用NSKeyedArchiver和NSKeyedUnarchiver,可以在游戏进入后台时保存游戏状态,并在下次启动时恢复游戏进度。
  • 解档

  

//AppDelegate.m
@synthesize window=_window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   
    NSString* gameArchivePath = [self gameArchivePath];
    OneGame* existingGame;
    @try {
        existingGame = [[NSKeyedUnarchiver unarchiveObjectWithFile:gameArchivePath] retain];
    }
    @catch (NSException *exception) {
        existingGame = nil;
    }
    [gameController setPreviousGame:existingGame];
    [existingGame release];
    
    //设置为主窗口并显示出来
    [self.window makeKeyAndVisible];
    return YES;
}



//GameController.m
-(void)setPreviousGame:(OneGame*)aOneGame{
    previousGame = [aOneGame retain];
    
    if (previousGame != nil && [previousGame remaingTurns] > 0){
        [continueButton setHidden:NO];
    } else {
        [continueButton setHidden:YES];
    }
}

 

  • 存档

 

//AppDelegate.m
//进入后台游戏存档
- (void)applicationDidEnterBackground:(UIApplication *)application
{
    NSString* gameArchivePath = [self gameArchivePath];
    [NSKeyedArchiver archiveRootObject:[gameController currentGame] toFile: gameArchivePath];
}

 

转载于:https://www.cnblogs.com/HackHer/p/8158994.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值