开发者

Extremely Simple Saved High Score

开发者 https://www.devze.com 2023-03-28 08:21 出处:网络
The game that I am creating has a highScore integer variable that gets assigned when the player loses. Iam using NSUsersDefaults class to save my high score. Here is my code that I am using:

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:

-(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 releas开发者_Go百科e];

    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


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.

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.

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.

- (void)viewDidLoad:
{
...
self.scoresView.textLabel.text = [NSString stringWithFormat:@"%d", highScore];
...
}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消