屏幕消失后存储结果

屏幕消失后存储结果

本文介绍了屏幕消失后存储结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款游戏,因为我想连续添加积分,因为我使用了plist但是每当屏幕消失并开始时plist再次启动。该怎么办?

I am developing a game,in that i want to add the points continuously,for this i used plist but whenever the screen is disappear and starts then plist starts again.what to do?

提前致谢。

推荐答案

要向Ahmed的答案添加更多信息,您应该在AppDelegate.m中实现三种方法:

To add more info to Ahmed's answer you should implement in your AppDelegate.m three methods like this:

AppDelegate.h

NSNumber *gamescore;

@property(nonatomic, strong) NSNumber *gamescore;


#define UIAppDelegate \
   ((AppDelegate *)[UIApplication sharedApplication].delegate)

AppDelegate.m

@synthesize gamescore;

- (BOOL) checkFirstRun {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSNumber *defaultcheck;
    defaultcheck = [defaults objectForKey:@"GameScore"];
    if (defaultcheck==nil) {
        return TRUE;
    } else {
        return FALSE;
    }
}

- (void) storeGlobalVars {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:gamescore forKey:@"GameScore"];
    [defaults synchronize];
}

- (void) readGlobalVars {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    gamescore = [defaults objectForKey:@"GameScore"];
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
// ...
    if ([self checkFirstRun]) {
    // first run, lets create basic default values

        gamescore = [NSNumber numberWithInt:0];
        [self storeGlobalVars];
    } else {
        [self readGlobalVars];
    }
// ...

稍后在您的应用程序中,导入后 AppDelegate.h 您可以使用 UIAppDelegate.gamescore 访问AppDelegate的属性。

Later in your application, after importing AppDelegate.h you can use the UIAppDelegate.gamescore to access the AppDelegate's property.

你必须记住gamescore是 NSNumber 对象,您必须使用 NSNumber numberWithInt 和/或 intValue 来操作它。

And you have to remember that gamescore is an NSNumber object, you have to manipulate it using NSNumber's numberWithInt and/or intValue.

需要 CheckFirstRun ,因为应用程序首次运行时用户的设备不包含默认plist和初始值,您必须创建初始设定。

The CheckFirstRun is needed because your user's device at application first run doesn't contain the default plist and the initial values, you have to create an initial set.

这篇关于屏幕消失后存储结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 21:39