如何在createWithSpriteFrameName中使用动态精灵名称?(How to use dynamic sprite names with createWithSpriteFrameName?)
我想根据Cocos2d-x和C ++中的一些动态变量更改精灵图像,所以我尝试这样的东西,但是有一个错误。 这可能是一种正确的方法吗? 问候。
auto name = String::createWithFormat("x%i.png", _destroyedUnits); auto combo = Sprite::createWithSpriteFrameName(name);我已经看到createWithSpriteFrameName接受C ++ basic_string,而不是Cocos String,所以我创建了这样的输出是“名字是:??”
int _destroyedUnits = 20; std::string intToString = std::to_string(_destroyedUnits); std::string name("x" + intToString); CCLOG("The name is: %s", name);什么是正确的方法呢? 问候。
I want to change a sprite image according to some dynamic variable in Cocos2d-x and C++, so I'm, trying something like this but there is an error. What could it be a right way to do it?. Greetings.
auto name = String::createWithFormat("x%i.png", _destroyedUnits); auto combo = Sprite::createWithSpriteFrameName(name);I've seen that createWithSpriteFrameName accept a C++ basic_string, not a Cocos String, so I created like this and the output is "The name is: ??"
int _destroyedUnits = 20; std::string intToString = std::to_string(_destroyedUnits); std::string name("x" + intToString); CCLOG("The name is: %s", name);What's the right way to do it? Greetings.
最满意答案
正如cocos2d-x测试中所建议的那样,正确的方法是使用sprintf,你可以像这样使用它:
char tmp[30]; sprintf(tmp,"x%i.png", _destroyedUnits); std::string atlasName = tmp; auto combo = Sprite::createWithSpriteFrameName(atlasName.c_str());As suggested in cocos2d-x tests, the right way is to use sprintf, you can use it like this:
char tmp[30]; sprintf(tmp,"x%i.png", _destroyedUnits); std::string atlasName = tmp; auto combo = Sprite::createWithSpriteFrameName(atlasName.c_str());更多推荐
发布评论