Tiled : how to get object x,y?

Hi again,

I’m just getting started with Cocos2d-x and Tiled, and I’m stuck trying to get the x and y values of my playerSpawn object that I defined in the objects layer in Tiled. I want to use them to set the position of the player.

This is what I have :

_objGroup = _tileMap->getObjectGroup(“objs”);
_objMap= _objGroup->getObject(“playerSpawn”);
_x = _objMap.at(“x”);
_y= _objMap.at(“y”);

_x and _y are Values … So how do I convert them to int ? I tried .asInt(), but I get an error for that …

I’m using : VS2015 on Win10, Cocos2d-x 3.8, c++

Thanks !

We have a Programers Guide and you answer is here.

Yes. I have read the guide. But the information on tiled maps is very brief. There is no sample code on how to get the coordinates of an object in the map.

I have also found some other tuts (eg/ www.raywenderlich.com/39113/cocos2d-x-tile-map-tutorial-part-1), but they seem to be for earlier versions of Cocos2d-x and so they don’t work with 3.9 or 3.8 … :frowning:

Just to be sure, you are NOT saying get a specific tile, you ARE saying get the coordinates of something on the map, like a Sprite?

in cpp-tests, TileMapTest.cpp has a lot of functionality.

If you send in a PR to modify this section in the Programmers Guide, I will merge it for the v3.10 launch. Can you help us make this better?

In Tiled, you have different kinds of layers. One type of layer is used for placing tiles (tile layer), another type of layer (object layer) is used for placing ‘regions’. This could be 1, or a bunch of tiles. The interesting thing is that you can name that region and then refer to it from your code.

Thanks for pointing me to the cpp-tests. I wouldn’t have thought of checking those for information.
And I would love to help … but : what’s a PR and how do I send one ? :smile:

What type is your map? Ortho, Iso?
Your asInt error should only occur if you have a “null” object. You can check Value for null using isNull().

// Our Isometric map code to get region in tile-space 
// rect in tile coords (w == 2 => region of 2 tile width)
Rect STMap::tileRectFromObject(const ValueMap& object)
{
    float tileHeight = this->getTileSize().height;

    auto group = this->getObjectGroup(groupName);
    // if(group) {
    auto obj = tileGroup->getObject(tileId);
    // if(! obj.empty()) {
    auto objType = obj.at("type").asString();
    auto objName = obj.at("name").asString();

    // size of region/object in points
    float height = object.at("height").asFloat();
    float width = object.at("width").asFloat();
    // one could acquire value asInt() instead, but float makes sense 
    // since Tiled exports as floats (you can position between tile coords
    float xOff = object.at("x").asFloat();
    float yOff = object.at("y").asFloat();

    height = height / tileHeight - 1.f;
    width = width / tileHeight - 1.f;
    xOff = xOff / tileHeight;
    // y is coord top-down instead of bottom-up (or vice versa)
    yOff = this->getMapSize().height - yOff / tileHeight - (height + 1.f);

    //dlog2("room object: %s, %.2f,%.2f,%.2f,%.2f", roomName.c_str(), height,width,xOff,yOff);

    return Rect(floorf(xOff), floorf(yOff), floorf(width), floorf(height));
}