Tried to fix propertiesForGID

So… propertiesForGID was always returning undefined. I was pretty much sure I was doing something wrong.

Then I found out this:

Well… it’s a bug, right? I decided to take a look at the code, and try to fix it (although I’m not so skilled in javascript).

A couple hours later, I was pretty much sure the problem was CCTMXXMLParser.js line 523:

var info = this._tileSets[0]; // <------------ HERE

As far as I could understand, it was considering only the first tileset, so after the second tileset the first GID would always be wrong.

And then, I tried to rewrite the code… it’s working, but I’m not so sure if I broke anything else or if I did it in the most efficient way… anyway, here is my fix

Replace this:

// PARSE  
var tiles = map.querySelectorAll('tile');
if (tiles) {
    for (i = 0; i < tiles.length; i++) {
        var info = this._tileSets[0];
        var t = tiles[i];
        this.setParentGID(parseInt(info.firstGid) + parseInt(t.getAttribute('id') || 0));
        var tp = t.querySelectorAll("properties > property");
        if (tp) {
            var dict = {};
            for (var j = 0; j < tp.length; j++) {
                var name = tp[j].getAttribute('name');
                var value = tp[j].getAttribute('value');
                dict[name] = value;
            }
            this._tileProperties[this.getParentGID()] = dict;
        }
    }
}

With this:

var tilesets = map.getElementsByTagName('tileset');
for (i = 0; i < tilesets.length; i++) {
    var selTileset = tilesets[i];
    var tiles = selTileset.getElementsByTagName('tile');

    if (tiles) {
        tileSetFirstGid = parseInt(selTileset.getAttribute('firstgid')) || 1;

        for (j = 0; j < tiles.length; j++) {
            var t = tiles[j];

            this.setParentGID(parseInt(tileSetFirstGid) + parseInt(t.getAttribute('id') || 0));

            var tp = t.querySelectorAll("properties > property");
            if (tp) {
                var dict = {};
                for (var k = 0; k < tp.length; k++) {
                    var name = tp[k].getAttribute('name');
                    var value = tp[k].getAttribute('value');
                    dict[name] = value;
                }
                this._tileProperties[this.getParentGID()] = dict;
            }
        }
    }            
}