尝试在Cocos2d-X C++中设置一个带有整数的CCLabelTTF作为其字符串的一部分

Dav*_*all 6 c++ string android cocos2d-x cclabelttf

所以在使用Cocos2d的Objective-C中,我使用带有格式的NSMutableString将变量(得分)放入字符串中.我拿这个字符串并使用CCLabel将它放在屏幕上.

使用Cocos2D-x,我很难找到获得此结果的方法.一个简单的例子就是很棒.谢谢!

m.d*_*ing 12

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);
Run Code Online (Sandbox Code Playgroud)


Coy*_*ote 5

在任何给定时间(从此处)设置字符串的更简单的解决方案.首先在代码中的某处定义一个宏.

#define ccsf(...) CCString::createWithFormat(__VA_ARGS__)->getCString()
Run Code Online (Sandbox Code Playgroud)

然后您可以随时更改字符串,如下所示:

m_pScoreLabel->setString(ccsf("%d pts", mCurrentScore));
Run Code Online (Sandbox Code Playgroud)