Trying to set up a CCLabelTTF with an integer as part of it's string in Cocos2d-X C++

∥☆過路亽.° 提交于 2019-12-23 10:00:17

问题


So in Objective-C with Cocos2d, I'd use a NSMutableString with format to put a variable (score) into a string. I'd take that string and use a CCLabel to place it on the screen.

Using Cocos2D-x, I'm having trouble finding a way to get this result. A simple example would be great. Thanks!


回答1:


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);



回答2:


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

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

Then you can change the string any time like this:

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


来源:https://stackoverflow.com/questions/12506187/trying-to-set-up-a-cclabelttf-with-an-integer-as-part-of-its-string-in-cocos2d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!