2017-08-14 130 views
1

我已经在瓷砖的地图编辑器中定义的属性为tag,以及如何检索在cocos2dxcocos2dx投的价值属性字符串

这是我如何做到这一点:

for (int h=0; h< size.height; h++){ 
    for (int w=0;w<size.width; w++){ 
     auto sprite = layer->tileAt(Vec2(w,h)); 
     auto gid = layer->getTileGIDAt(Vec2(w,h)); 
     Value v = tmap->getPropertiesForGID(gid); 
     ValueMap m = v.asValueMap(); 
     auto tag = m.at("tag"); 
     if (!tag.isNull()) { 
      CCLOG(" tag string is %s", tag.asString());  
      } 
    } 
} 

我期待输出到像blue, gray, black....这是我放在平铺精灵的属性标签,但实际出一些东西一样:

tag string is ?SEgray 

代替邻˚F

tag string is gray 

像 有些事情我也试过这样:

tag.asString(); 

但是,这并不证明是正确的要么。

回答

0

我想通了这一点

我应该使用这种

std::string type = tag.asString().c_str(); 

投给最后一个字符串,不知道什么是.c_str()是,但它的工作。思想应该放在这里帮助一个人遇到同样的问题。

+0

嗯,这很奇怪,因为['Value :: asString()'](http://www.cocos2d-x.org/reference/native-cpp/V3.0rc0/dc/dd1/classcocos2d_1_1_value.html# aa8535ea1f8f4f88ba32dcb36b6779ff2)已经返回一个'std :: string',所以使用['c_str()'](http://www.cplusplus.com/reference/string/string/c_str/)只会让它慢一点,因为新的'std :: string type'将需要再次确定字符串的长度。你只需要在'CCLOG'语句中调用'c_str',这个语句可能像'printf'那样工作,不能处理'std :: string'('%s'需要'char *')。 –

+0

@ThorbjørnLindeijer很高兴见到你StackOverflow的男人,真的很奇怪:D – armnotstrong