Save the high score cocos2d

2019-09-16 07:18发布

问题:

I try to explain better the situation.

The variables are:

int punteggio;

CCLabelTTF *labelPunteggio;

Then in the init metod i print my score on the screen:

- (id)init {
    if ((self = [super init])) {

    // PUNTEGGIO
    labelPunteggio = [CCLabelTTF labelWithString:@"0000" fontName:@"Marker Felt" fontSize:13];

    [self addChild:labelPunteggio];
    ....
    }
}

And this is the function to add score on Punteggio: for example, every time i kill a monster i add 10 point.

-(void)aggiungiPunti
{
    punteggio = punteggio +0001;

    [labelPunteggio setString:[NSString stringWithFormat:@"%d", punteggio]];
}

But now, i don't know how save the score when the player do game over. I'd want save this score, and then print the high score on the screen, i think about

-(void) setScore:(int)score
{
    punteggio = highScore;

    if (punteggio>highScore)
    {
        highScore = punteggio;
    }
}

Thank you!

回答1:

Use NSUserdefaults

// Snippet used to save your highscore in the prefs.
int highScore  = yourGameScore;
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:highScore] forKey:@"HighScore"];
[[NSUserDefaults standardUserDefaults] synchronize];

//In Game Over screen

// Get your highscore from the prefs.
highScore = [[[NSUserDefaults standardUserDefaults] objectForKey:@"HighScore"] intValue ];


回答2:

look at this link and you can use SettingManager class to do this work for you. i have used settingManager class for storing highscore. Hope this will help