How to use dynamic sprite names with createwithspriteframename?

Hi there, I want to change a sprite image according to some dynamic variable, so I’m, trying something like this but there is an error. What could it be a right way to do it?. Greetings.

if(_destroyedUnits>0);
    auto name = String::createWithFormat("x%i.png", _destroyedUnits);
    auto combo = Sprite::createWithSpriteFrameName(name); 
}

I’ve seen that createWithSpriteFrameName accepts a C++ basic_string, not a Cocos String, so the way is with string and not String class.

int _destroyedUnits = 4;
std::string intToString = std::to_string(_destroyedUnits);
std::string name("x" + intToString + ".png");  
auto combo = Sprite::createWithSpriteFrameName(name);

Greetings.

Concisely :smiley:

// `using std::to_string` at top of scope/class/namespace/file
auto combo = Sprite::createWithSpriteFrameName("x" + to_string(_destroyedUnits) + ".png");
1 Like