High score and current score

回眸只為那壹抹淺笑 提交于 2019-12-25 02:06:51

问题


I want to show on the screen the current score of the gameplay and the storical best score. It is work, but every times i restart the game the best score change even if the current score is lower than the best score.

CCLabelTTF *punteggio;
    NSString *stringa;
    NSString *stringa2;
    CCLabelTTF *punteggioMAX;

    int score;
    int scoreMAX;

There are the methods to SAVE the score, to add the score and to reset the score at the end of the game.

-(void)aum{
    score++;
    stringa = [NSString stringWithFormat:@"Punteggio: %d",score];
    [punteggio setString:stringa];
}



-(void)res{
    score=0;
    stringa = [NSString stringWithFormat:@"Punteggio: %d",score];
    [punteggio setString:stringa];
}

-(void)sal{
    NSUserDefaults *ud=[NSUserDefaults standardUserDefaults];
    [ud setInteger:score forKey:@"Punteggio"];
    [ud synchronize];
}

-(void)sal2{
    NSUserDefaults *ud=[NSUserDefaults standardUserDefaults];
    [ud setInteger:scoreMAX forKey:@"Punteggio"];
    [ud synchronize];
}

And in the init method:

 NSString *fontName = @"score.fnt";
        stringa = [NSString stringWithFormat:@"Punteggio: %d",score];
        punteggio = [CCLabelBMFont labelWithString:stringa fntFile:fontName];
        punteggio.scale = 0.4;
        punteggio.position=ccp(40,altezzaSchermo - 15);
        [self addChild:punteggio];
        NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
        score=[ud integerForKey:@"Punteggio"];



        stringa2 = [NSString stringWithFormat:@"Best Score: %d",score];
        punteggioMAX = [CCLabelBMFont labelWithString:stringa2 fntFile:fontName];
        punteggioMAX.scale = 0.4;
        punteggioMAX.position=ccp(40,altezzaSchermo - 35);
        [self addChild:punteggioMAX];
        scoreMAX=[ud integerForKey:@"punteggioMAX"];


 if(score>scoreMAX) scoreMAX = score;

        [self res];

Thank you.


回答1:


You aren't saving punteggioMAX therefore it will return 0 when you retrieve it from user defaults.

Easy to verify: set a breakpoint, check the variable.



来源:https://stackoverflow.com/questions/15692877/high-score-and-current-score

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!