1

I have defined a property as tag in tiled map editor, and what to retrieve that in cocos2dx

Here is how I do that:

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());      
          }
   }
}

I was expecting the output to be like blue, gray, black.... which is the tag I put in the properties of the tiled sprite, but the actually out is some thing like:

   tag string is ?SEgray

instead of

  tag string is gray

Some thing like that I also tried this:

  tag.asString();

But that doesn't turn out to be right either.

armnotstrong
  • 8,605
  • 16
  • 65
  • 130

1 Answers1

0

I figured this out

I should use this

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

to cast to the final string, not sure what's .c_str() is, but it worked. Thought should put it here to help some one meet with the same issue.

armnotstrong
  • 8,605
  • 16
  • 65
  • 130
  • Hmm, that's very strange because [`Value::asString()`](http://www.cocos2d-x.org/reference/native-cpp/V3.0rc0/dc/dd1/classcocos2d_1_1_value.html#aa8535ea1f8f4f88ba32dcb36b6779ff2) already returns a `std::string`, so using [`c_str()`](http://www.cplusplus.com/reference/string/string/c_str/) on it would only make this a little slower, since the new `std::string type` will need to determine the length of the string again. You should need the call to `c_str` only in the `CCLOG` statement, which likely works like `printf` and can't handle `std::string` (`%s` needs a `char*`). – Thorbjørn Lindeijer Aug 14 '17 at 08:10
  • @ThorbjørnLindeijer nice to meet you on StackOverflow man, truely, it is strange :D – armnotstrong Aug 14 '17 at 08:19