如何在游戏中存储高分

时间:2014-05-13 07:49:50

标签: c++ cocos2d-x

我正在使用cocos2d-x在xcode中开发游戏。我希望在游戏中显示场景中的高分。当我运行游戏时,即使得分低于高分,分数也会在场景中显示而不是高分。我认为高分不存储。我使用以下代码。请帮我解决这个问题。

CCUserDefault *def=CCUserDefault::sharedUserDefault();
long int high_score=0; 
if(score>high_score)
{
    def->setIntegerForKey(HIGH_SCORE, score);
    //def->flush();
    //high_score=def->getIntegerForKey(HIGH_SCORE);
}
else if(score<high_score)
{
    def->setIntegerForKey(HIGH_SCORE, high_score);
    //def->flush();
    //high_score=def->getIntegerForKey(HIGH_SCORE);
}
high_score=def->getIntegerForKey(HIGH_SCORE);

char s[7];
sprintf(s,"%ld", high_score);
CCLabelTTF *high_label=CCLabelTTF::create(s, "Arial.fnt", 20);
high_label->setPosition(ccp(winwsize - 800, winhsize - 50));
this->addChild(high_label,2);

1 个答案:

答案 0 :(得分:0)

high_core变量设为全局。

请注意,如果score<high_score确实不需要任何内容​​,那么您可以删除def->setIntegerForKey(HIHG_SCORE, high_score);语句。