Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 为什么赢了';难道我的高分救不了你吗?_Ios_Objective C_Cocos2d Iphone_Store - Fatal编程技术网

Ios 为什么赢了';难道我的高分救不了你吗?

Ios 为什么赢了';难道我的高分救不了你吗?,ios,objective-c,cocos2d-iphone,store,Ios,Objective C,Cocos2d Iphone,Store,我真的不明白为什么我的高分不能挽救。作为序言,这里是我试图完成的一般任务 如果分数大于高分,则高分将成为分数 如果用户退出应用程序,高分数据将保持不变 当用户游戏失败时,在屏幕上显示高分 这是相关代码 在执行中: int highScore; NSUserDefaults * defaults; 在初始化中: defaults = [NSUserDefaults standardUserDefaults]; 在代码主体中: scoreLabelNumber = [CCLabelTTF

我真的不明白为什么我的高分不能挽救。作为序言,这里是我试图完成的一般任务

  • 如果分数大于高分,则高分将成为分数
  • 如果用户退出应用程序,高分数据将保持不变
  • 当用户游戏失败时,在屏幕上显示高分
这是相关代码

在执行中:

int highScore;
NSUserDefaults * defaults;
在初始化中:

 defaults = [NSUserDefaults standardUserDefaults];
在代码主体中:

 scoreLabelNumber = [CCLabelTTF labelWithString:myScore fontName:@"Helvetica" fontSize:25.0f];
scoreLabelNumber.color = [CCColor whiteColor];
scoreLabelNumber.position = ccp(self.contentSize.width / 2 + 35, self.contentSize.height - 95);

if(alive == FALSE){

    if(score > highScore){

        highScore = score;

        [[NSUserDefaults standardUserDefaults]setInteger:score forKey:@"highscrore"];
        [defaults synchronize];

        [[NSUserDefaults standardUserDefaults] synchronize];


    }

}
高分将显示,但如果我退出应用程序,则不会保存有关此高分的信息。如何解决此问题?

看起来您“忘记”将值加载回highScore变量:

highScore = [defaults integerForKey:@"highscrore"];
请注意:

    [[NSUserDefaults standardUserDefaults]setInteger:score forKey:@"highscrore"];
    [defaults synchronize];

    [[NSUserDefaults standardUserDefaults] synchronize];
可以缩短为:

    [defaults setInteger:score forKey:@"highscrore"];
    [defaults synchronize];
你可能想修正一下“highscrore”的拼写错误