Question

I am developing a game in xcode using cocos2d-x. I want to display the highscore in game over scene.when i run the game, score is displaying in the game over scene instead of highscore, even if the score is less than highscore. I think highscore is not stored. I am using the following code. Please help me to solve this.

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);
Was it helpful?

Solution

Make the high_core variable global.

Pay attention that if score<high_score nothing is really needed so you can drop the def->setIntegerForKey(HIHG_SCORE, high_score); statement.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top