【问题标题】:How to use dynamic sprite names with createWithSpriteFrameName?如何在 createWithSpriteFrameName 中使用动态精灵名称?
【发布时间】:2016-10-27 15:44:05
【问题描述】:

我想根据 Cocos2d-x 和 C++ 中的一些动态变量更改精灵图像,所以我正在尝试这样的事情,但出现错误。什么是正确的方法呢?问候。

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

我看到 createWithSpriteFrameName 接受 C++ basic_string,而不是 Cocos 字符串,所以我这样创建,输出是“名称是:??”

int _destroyedUnits = 20;
std::string intToString = std::to_string(_destroyedUnits);
std::string name("x" + intToString);
CCLOG("The name is: %s", name);

正确的做法是什么?问候。

【问题讨论】:

  • 问题有什么问题?

标签: c++ c++11 cocos2d-x


【解决方案1】:

在cocos2d-x测试中建议,正确的方法是使用sprintf,你可以这样使用:

char tmp[30];
sprintf(tmp,"x%i.png", _destroyedUnits);
std::string atlasName = tmp;
auto combo = Sprite::createWithSpriteFrameName(atlasName.c_str());

【讨论】:

    【解决方案2】:

    我已经看到 createWithSpriteFrameName 接受 C++ basic_string,而不是 Cocos String,所以方法是使用字符串而不是 String 类。

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

    您好。

    【讨论】:

      猜你喜欢
      • 2019-10-17
      • 1970-01-01
      • 2015-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多