Jump to content

Way to get GID in Tiled for use in Phaser?


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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

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