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 7415611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:20:09+00:00 2026-05-29T07:20:09+00:00

Please can anyone refer me to a tutorial or help me out with this

  • 0

Please can anyone refer me to a tutorial or help me out with this problem. I wish to display high scores and other games statistics at game Over but I don’t seem to get a headway despite looking at tutorials and trying stuff out. I don’t seem to understand how to use NSUserdefaults to achieve this.

My codes are as follows:

Gameplaylayer.mm

- (id)initWithHUDLayer:(HUDLayer *)hudLayer {
    if ((self = [super init])) 
    {
        score = 0;
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSDictionary *defaultArray = [NSDictionary dictionaryWithObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:0],
                                                                        [NSNumber numberWithInt:0],
                                                                        [NSNumber numberWithInt:0],
                                                                        [NSNumber numberWithInt:0],
                                                                        [NSNumber numberWithInt:0],
                                                                        nil]
                                                                forKey:@"Scores"];
        [defaults registerDefaults:defaultArray];
        [defaults synchronize];
    }

    return self;
}


-(void)gameOver:(id)sender{

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    NSMutableArray *HighScores = [NSMutableArray arrayWithArray:[defaults arrayForKey:@"Scores"]];

    for (unsigned int i = 0; i < [HighScores count]; i++)
    {
        if (dist >= [[HighScores objectAtIndex:i] intValue])
        {
            [HighScores insertObject:[NSNumber numberWithInt:dist] atIndex:i];
            [HighScores removeLastObject];

            [defaults setObject:HighScores forKey:@"Scores"];
            [defaults synchronize];

            NSLog(@"Saved new high score of %i", dist);
            break;
        }
    }
}

-(void)update:(ccTime)dt {

    CGSize winSize = [CCDirector sharedDirector].winSize;

    score += (int)delta;
    dist = score - 18;

    if (!gameOver ) 
    {
        if (!lives)
        {
            gameOver = true;
            [self gameOver];    
        }
    }
}

my game over layer is as follows

-(id)init {
    self = [super init];
    if (self != nil) 
    {
        self.isTouchEnabled = YES;

        CGSize windowSize = [CCDirector sharedDirector].winSize;

        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

        NSArray *HighScores = [defaults arrayForKey:@"Scores"];

        CLabelTTF *title = [CCLabelTTF labelWithString:@"high scores" fontName:@"Courier" fontSize:32.0];

        [title setPosition:ccp(screenSize.width / 2, screenSize.height - title.contentSize.height)];

        [self addChild:title];

        NSMutableString *scoresString = [NSMutableString stringWithString:@""];

        for (unsigned int i = 0; i < [HighScores count]; i++)
        {
            [scoresString appendFormat:@"%i. %i\n", i + 1, [[HighScores objectAtIndex:i] intValue]];
        }

        CCLOG(@"scores saved %i", dist);

        CCLabelTTF *scoresLabel = [CCLabelTTF labelWithString:scoresString dimensions:CGSizeMake(screenSize.width, screenSize.height / 3) alignment:CCTextAlignmentCenter fontName:@"Courier" fontSize:40.0];

        [scoresLabel setPosition:ccp(screenSize.width / 2, screenSize.height / 2)];

        scoresLabel.color = ccc3(0, 0, 0);
        [self addChild:scoresLabel z:9];
    }
    return self;
}
  • 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-29T07:20:10+00:00Added an answer on May 29, 2026 at 7:20 am

    It’s hard to understand your code, please look at this small example below.

    Ok, that’s saving new score, assuming you have GameController singleton, where scores mutable array is declared and currentScore in that singleton also displays current player’s score.

    -(void) saveScores {
    
        GameController * controller = [GameController sharedController];
    
        for(int i=0; i<5;i++)
        {
            if([[[controller.scores objectAtIndex:i] objectForKey:@"score"] intValue] == controller.finalscore)
            {
                break;
            }
            if([[[controller.scores objectAtIndex:i] objectForKey:@"score"] intValue] < controller.finalscore) {
                for(int j=5;j>i+1;j--) 
                {
                    [controller.scores replaceObjectAtIndex:j-1 withObject:[controller.scores objectAtIndex:j-2]];
                }
    
                NSMutableDictionary *newEntry = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:controller.finalscore],@"score",@"Local player",@"name", nil];
                [controller.scores replaceObjectAtIndex:i withObject:newEntry];
                break;
            }
        }
        controller.currentScore=0;
    
        [[NSUserDefaults standardUserDefaults] setObject:controller.scores forKey:@"OurScores"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    

    You can see, that we are saving updated scores in NSUserDefaults. So, at our game start we shoul load them. I added this code to GameController init method:

    - (void) loadScores {
        if([[NSUserDefaults standardUserDefaults] objectForKey:@"OurScores"] == nil) 
        {
            NSMutableDictionary *empty = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:0],@"score",@"empty",@"name", nil];
            for(int i=0;i<5;i++)
                [scores addObject:empty];
        } 
        else 
        {
            NSLog(@"Loading scores");
            scores = [[NSUserDefaults standardUserDefaults] objectForKey:@"OurScores"];
        }
    }
    

    And that’s how we can show this scores using cocos2d:

    -(void) drawScores {
        for(int i=0; i<5; i++)
        {
            CCLabelTTF* score = [CCLabelTTF labelWithString:
                                 [NSString stringWithFormat:@"%@",[[controller.scores objectAtIndex:i] objectForKey:@"name"]] fontName:@"Marker Felt" fontSize:30];
            score.anchorPoint=ccp(0,0);
            score.position = ccp(115, 180-i*35);
    
            CCLabelTTF* score2 = [CCLabelTTF labelWithString:
                                  [NSString stringWithFormat:@"%d",[[[controller.scores objectAtIndex:i] objectForKey:@"score"] intValue] ] fontName:@"Marker Felt" fontSize:35];
            score2.anchorPoint=ccp(0,0);
            score2.position = ccp(280, 180-i*35);
    
            score.color=currentClr;
            score2.color=currentClr;
    
            [self addChild: score z:3 tag:i];
            [self addChild: score2 z:3 tag:i+15];
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please can anyone help me out with this problem? This code below works perfectly
Please refer to http://m.xn--e-nsker-r1a.dk/ to see the problem in real life. Can anyone tell
Can anyone please let me know what widget is this? Please refer to the
Can anyone please refer any tutorial for writing text over images taken from SD
Please can anyone help me with this? I have 2 tables, location and tickets
Please can anyone one point me to a good tutorial that helps me to
Has anyone else had this problem? I can find absolutely nothing when I search
HI all can anyone please refer me, the iphone image gallery source code? Any
I need to update jquery1.3.2 to jquery1.4 (in Asp.net MVC). Please can anyone tell
can anyone please suggest a good code example of vb.net/c# code to put the

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.