[SOLVED] Bug in CCTMXXMLParser, incorrect CCTMXObjectGroup Y axis values with cocos2dx 2.1.1 ?

[SOLVED] Bug in CCTMXXMLParser, incorrect CCTMXObjectGroup Y axis values with cocos2dx 2.1.1 ?
0.0 0



Been playing with CCTMXTiledMap and seem to have encountered a bug in CCTMXObjectGroup parsing.
Using latest stable cocosdx 2.1.1 and Tiled Map Editor 0.9.0

For example, with following TMX data:

The returned Y axis value is always incorrect, at exception of first object.

Object x=32 y=160 width=32 height=160
Object x=384 y=352 width=448 height=32
Object x=800 y=384 width=32 height=96
Object x=384 y=64 width=224 height=32
Object x=800 y=160 width=160 height=32
Object x=928 y=0 width=32 height=160
Object x=64 y=160 width=192 height=32
Object x=256 y=192 width= height=
Object x=608 y=96 width= height=
Object x=64 y=288 width= height=

I’m parsing the object group like following, please comment if you spot something wrong in the code:

    // Load tilemap
    this->pMap = CCTMXTiledMap::create("maps/level1.tmx");
    this->addChild(this->pMap, -1);

    // Iterate object groups
    CCObject *groups;
    CCARRAY_FOREACH(this->pMap->getObjectGroups(), groups)
        // object groups (object layers)
        CCTMXObjectGroup *group = dynamic_cast(groups);
        if(group != NULL)
            // Found collision group
            if(strcmp("Collision", group->getGroupName()) == 0)
                // Iterate objects
                CCObject *items;
                CCARRAY_FOREACH(group->getObjects(), items)
                    CCDictionary * object = dynamic_cast(items);
                    if(object != NULL)
                        // Get values
                        CCString* x = (CCString *)object->valueForKey("x");
                        CCString* y = (CCString *)object->valueForKey("y");
                        CCString* width = (CCString *)object->valueForKey("width");
                        CCString* height = (CCString *)object->valueForKey("height");

                        CCLog("Object x=%s y=%s width=%s height=%s", x->getCString(), y->getCString(), width->getCString(), height->getCString());


I’ll have a look into CCTMXXMLParser source as well and will post if find something wrong with it.

level1.tmx.zip (0.9 KB)

SceneTileMapBox2D.h.zip (0.6 KB)

SceneTileMapBox2D.cpp.zip (2.6 KB)


Ok, so cocos2dx inverts the Y axis because TMX Y axis is inverted (0 top instead of 0 bottom).
But in that case shouldn’t the first Object have its Y axis inverted during the parsing ?


        // Y
        value = valueForKey("y", attributeDict);
        if( value )  {
            int y = atoi(value) + (int)objectGroup->getPositionOffset().y;

            // Correct y position. (Tiled uses Flipped, cocos2d uses Standard)
            y = (int)(m_tMapSize.height * m_tTileSize.height) - y - atoi(valueForKey("height", attributeDict));
            sprintf(buffer, "%d", y);
            CCString* pStr = new CCString(buffer);
            dict->setObject(pStr, "y");


No bug, Y axis is inverted as expected… once more should have investigated more before posting to forum :slight_smile:


I have exactly this problem now, but reading these posts I´m not sure how the issue was solved. The y values saved in the TMX file from Tiled and the y values returned from code are not the same. Reading this post I understand that this has something to do with an inverted y-axis, but I don´t get it completely. What exactly do I need to do to get around this? It would be very helpful if someone could elaborate on this.

I am working with an isometric map 50 x 50 tiles using tilesize 32x16 px. I want to spawn sprites at object locations saved in the TMX map in an object layer.
As far as I understand the x and y values that are stored in my TMX file are pixel locations in isometric space? So my first step is to get the tile coordinates using these x and y values: For now I´m able to find tile-x coordinate using x / tile height = x / 16. Don´t know what to do with the strange y value? The Y value saved the tmx file would work with y / 16, but the problem is of course that the y value has “magically” changed when reading it using object->valueForKey(“y”).

In order to find the screen position I am planning to use CCTMXLayers function: CCPoint positionAt (const CCPoint & tileCoordinate).