保存高分cocos2d [英] Save the high score cocos2d

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

问题描述

我试图更好地解释这种情况.

I try to explain better the situation.

变量为:

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

这是在Punteggio上增加得分的功能:例如,每杀死一个怪物我都会加10点.

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

谢谢!

推荐答案

使用NSUserdefaults

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

这篇关于保存高分cocos2d的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆