本文介绍了高分和当前分数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在屏幕上显示游戏的当前得分和历史最高得分.这是有效的,但每次我重新开始游戏时,即使当前分数低于最佳分数,最佳分数也会改变.

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];
}

在init方法中:

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

谢谢.

推荐答案

您没有保存 punteggioMAX 因此,当您从用户默认值中检索它时,它将返回 0.

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.

这篇关于高分和当前分数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 09:27