Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6331061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:59:11+00:00 2026-05-24T17:59:11+00:00

The game that I am creating has a highScore integer variable that gets assigned

  • 0

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 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T17:59:11+00:00Added an answer on May 24, 2026 at 5:59 pm

    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];
    ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a database that tracks replays for a game. Each replay has
I'm creating a game that uses cards. I have an AppController class with one
I am creating a card game in Silverlight. Each player has a Hand which
i'm creating a browser rpg game where the player has an inventory and a
This is a topic that has been bugging me recently. When creating a game
I'm planning on creating a game that contains a landscape with objects on it.
I'm interested in creating a game that uses fractal maps for more realistic geography.
I m creating a game, in that i want to use some animation, but
I am creating a Stratego Game and am having trouble creating an algorithm that
i'm creating a card game on iphone. My problem is that i want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.