Jump to content

Way to get GID in Tiled for use in Phaser?


tarr11
 Share

Recommended Posts

  • 6 months later...

I know this is an old post, but I got tired of calculating the GID and want to share my solution:

function tilePropertyToGid(value, map, property) {    var keys, i, i2;    if (typeof (property) === "undefined") {        property = "type";    }    for (i = 0; i < map.tilesets.length; i++) {        if (!(map.tilesets[i].hasOwnProperty("tileProperties"))) {            continue;        }        keys = Object.keys(map.tilesets[i].tileProperties);        for (i2 = 0; i2 < keys.length; i2++) {            if ((map.tilesets[i].tileProperties[keys[i2]].hasOwnProperty(property)) && (map.tilesets[i].tileProperties[keys[i2]][property] === value)) {                return (parseInt(keys[i2], 10) + parseInt(map.tilesets[i].firstgid, 10));            }        }    }    console.log("Error: No GID found!");    return false;} 

The function searches all tiles for a property matching a value/key and return the GID for that tile. Parameters is value for value to search for, map which is a tiled map and property that is the name of the property (default is "type"). You can add properties to tiles in Tiled by right-click a tile in the tile set-view and select "Tile properties". A good thing with this approach is that nothing will break if the GID would change for some reason.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...