Question

I am developing a game in cocos2d-x.While trying to update the score it is displaying 11 , I want the score should be keep on increasing. below i pasted my code please help.

 schedule(schedule_selector(HudLayer::updateScore));
    void HudLayer::updateScore(int score)
     {
         score=1;

            do {
                score=score+10;
                const int labelLength = 100;
          char scoreLabelText[labelLength];
                snprintf(scoreLabelText, labelLength, "Score: %d", score);
          scoreLabel->setString(scoreLabelText);
            } while (score<0);
Was it helpful?

Solution

The reason why your score is stuck at 11 is that at the first line of your function you set it to 1. It seems that you wanted to pass current score as an argument to this function, but it won't work like this with your schedule.

Example code for adding 10 points with every call of updateScore :

void HudLayer::updateScore() // no argument needed
 {
     //score=1; <- this would override current score, bug/not needed

        do { // unnecessary
            score=score+10;
            const int labelLength = 100;
      char scoreLabelText[labelLength];
            snprintf(scoreLabelText, labelLength, "Score: %d", score);
      scoreLabel->setString(scoreLabelText);
        } while (score<0); //unnecessary
}

// in your *.h file
class HudLayer : <your inheritance> {

    int score;
    //your code

}

Of course you should initialize the score to some value, but do it only once, for example in the constructor or init() method.

Let me know if anything is not clear.

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