Trying to configure CCLabelTTF with an integer as part of its string in Cocos2d-X C ++

So, in Objective-C with Cocos2d, I would use an NSMutableString with a format to put a variable (evaluation) in a string. I would take this line and use CCLabel to put it on the screen.

Using Cocos2D-x, I had trouble finding a way to get this result. A simple example will be great. Thanks!

+6
source share
2 answers
int score = 35; float time = 0.03; char* name = "Michael"; char text[256]; sprintf(text,"name is %s, time is %.2f, score is %d", name, time, score); CCLabelTTF* label = CCLabelTTF::labelWithString(text,"Arial",20); this->addChild(label); 
+12
source

A simpler solution for setting the string at any given time ( here ). First define a macro somewhere in your code.

 #define ccsf(...) CCString::createWithFormat(__VA_ARGS__)->getCString() 

Then you can change the line at any time as follows:

 m_pScoreLabel->setString(ccsf("%d pts", mCurrentScore)); 
+5
source

Source: https://habr.com/ru/post/925811/


All Articles