CCLabelTTF not working in Android

Hi,

I have NDK [Revision 7] Installed and whenever I compile my cocos2dx code for android with any label [CCLabelTTF] it crashes. And it doesn’t when I comment CCLabelTTF code.

I created a small flipcard game and I want to show score on top. For that I have one function that creates CCLabelTTF

// Show Score
void TCSceneGamePlay::showScore() {

    // Set Labels & Scores
    stringstream myStream;
    string myScore = "";

    // Conversion
    myStream << " Score: " << totalScore << "     Pair Remaining: " << totalNumberOfPairs;
    myScore = myStream.str();

    CCLabelTTF *myScoreTTF = CCLabelTTF::labelWithString(myScore.c_str(), "Arial", 22);
    myScoreTTF->retain();
    myScoreTTF->setTag(LABEL_SCORE);
    myScoreTTF->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width/2, CCDirector::sharedDirector()->getWinSize().height - 30));
    this->addChild(myScoreTTF, 1);
}

and I have another function which updates the score

// Update Score
void TCSceneGamePlay::updateScore() {

    // Set Score
    stringstream myStream;
    string myScore = "";

    // Conversion
    myStream << " Score: " << totalScore << "     Pair Remaining: " << totalNumberOfPairs;
    myScore = myStream.str();

    // Updating
    CCLabelTTF *myScoreTTF = (CCLabelTTF *) this->getChildByTag(LABEL_SCORE);
    myScoreTTF->setString(myScore.c_str());
}

Now, on each click event I call updateScore function. It works fine on iPhone but doesn’t work on Android.

Hi there,

I’m having the same problem here…It’s an issue with stringstream on android. If anyone knows a solution, please share =)

Hi again,

Try using sprintf instead…example:

char strBuffer [13];
sprintf(strBuffer, “Score: %d”,GameData::sharedGameData()->currentScore);
CCLabelTTF* pLabel = CCLabelTTF::labelWithString(strBuffer, “Thonburi”, 34);