xcode 极其简单的保存高分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7048175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Extremely Simple Saved High Score
提问by 64bitman
The game that I am creating has a highScore integer variable that gets assigned when the player loses. I am using NSUsersDefaults class to save my high score. Here is my code that I am using:
我正在创建的游戏有一个 highScore 整数变量,当玩家输球时会分配该变量。我正在使用 NSUsersDefaults 类来保存我的高分。这是我正在使用的代码:
-(void)saveScore {
[[NSUserDefaults standardUserDefaults] setInteger:score forKey:@"highScore"];
[defaults setInteger:score forKey:@"highScore"];
[defaults synchronize];
NSLog(@"High Score: %i ", highScore);
}
-(IBAction)buttonReleased:(id)sender {
[stopWatchTimer invalidate];
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
NSString *label0 = @"Hold to Start";
[labelText setText:label0];
if (score > 0) {
score--;
}
else {
score = 0;
NSLog(@"Changed score to 0");
}
if (score > highScore) {
[self saveScore];
NSString *scoreMessage =[[NSString alloc] initWithFormat:@"Congrats! You have a new High Score! Click Share High Score to share your score of: %i",score];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"High Score!" message:(NSString *)scoreMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
score = 0;
}
else {
NSString *scoreMessage =[[NSString alloc] initWithFormat:@"Game Over! Your score was: %i",score];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"GAME OVER!" message:(NSString *)scoreMessage delegate:nil cancelButtonTitle:@"Try Again" otherButtonTitles: nil];
[alert show];
[alert release];
score = 0;
}
- (void)viewDidLoad
{
[super viewDidLoad];
int highscore = [[NSUserDefaults standardUserDefaults] integerForKey: @"highScore"];
[stopWatchTimer invalidate];
stopWatchTimer=nil;
}
I have been wrestling with this for HOURS! What am I doing wrong?! Note: Can you explain it as simply as possible.
我一直在努力解决这个问题!我究竟做错了什么?!注意:你能不能尽可能简单地解释一下。
Thanks! -Matt
谢谢!-马特
回答by Oli
Reading it:
阅读它:
int highscore = [[NSUserDefaults standardUserDefaults] integerForKey: @"highScore"];
It will most likely be the default value of int (i.e. 0) when the file is blank.
当文件为空时,它很可能是 int 的默认值(即 0)。
Also don't forget to force a write of your defaults to "disk" with synchronize:
另外不要忘记使用同步强制将默认值写入“磁盘”:
-(void)saveScore {
NSUSerDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:score forKey:@"highScore"];
[defaults synchronize];
}
You can load the highscore either in viewDidLoad or even in your init (or initWithNibName) method since this part isn't dependent on your view being loaded.
您可以在 viewDidLoad 或什至在您的 init(或 initWithNibName)方法中加载高分,因为这部分不依赖于您正在加载的视图。
You could declare a property on your Scores view that you set in the viewDidLoad method. Alternatively you could expose the UILabel of that scores class (if that's what you use) as a property of your scores class.
您可以在您在 viewDidLoad 方法中设置的 Scores 视图上声明一个属性。或者,您可以将该分数类的 UILabel(如果这是您使用的)公开为您的分数类的属性。
- (void)viewDidLoad:
{
...
self.scoresView.textLabel.text = [NSString stringWithFormat:@"%d", highScore];
...
}
回答by fzaziz
There is a really simple highscore management system which I have written it even has online support. You can find it https://github.com/faizanaziz/HighScore
有一个非常简单的高分管理系统,我已经编写了它甚至有在线支持。你可以找到它https://github.com/faizanaziz/HighScore