2017-08-14 58 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