【发布时间】:2014-03-05 16:05:33
【问题描述】:
我正在使用 SpriteKit 制作游戏。我第一次输掉比赛,高分是0(好)。然后,我保存当前分数。下次我玩的时候,高分是一个很大的数字,例如 366508176。为什么会发生这种情况以及如何解决它?
这是 ScoreScene.m 文件,它的超类是 SKScene。 [GameScene getScore] 是返回最后得分的静态函数。
//
// ScoreScene.m
//
#import "ScoreScene.h"
#import "GameScene.h"
@interface ScoreScene()
@property BOOL contentCreated;
@end
@implementation ScoreScene
-(void)didMoveToView:(SKView *)view
{
if (!self.contentCreated) {
[self createSceneContents];
self.contentCreated=YES;
}
}
-(void)createSceneContents
{
self.backgroundColor=[SKColor purpleColor];
self.scaleMode=SKSceneScaleModeAspectFill;
NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
int highScore=(int)[userDefaults objectForKey:@"HighScore"];
//highScore is 0 the first time, 366508176 the second
int currentScore=[GameScene getScore];
//currentScore is 3, for example
if (currentScore>highScore) {
[userDefaults setInteger:currentScore forKey:@"HighScore"];
[userDefaults synchronize];
highScore=currentScore;
}
[self addChild:[self newHighScoreLabel:highScore]];
[self addChild:[self newScoreLabel]];
[self addChild:[self newStartLabel]];
}
...
@end
【问题讨论】:
标签: ios iphone objective-c nsuserdefaults