Jump to content

Tiled tile properties


beeglebug
 Share

Recommended Posts

If I add properties to a tile in Tiled, I can see the data on the TileSet (tileProperties), but the data isn't accessable anywhere.

 

I searched for tileProperties in the repo, and it looks like it's literally set and forgotten about.

 

Is there a reason the properties aren't copied to tiles, or that there are no accessors for the properties?

Link to comment
Share on other sites

Let me help you, i've just faced the same problem. Why are not props copied - dunno, i asked myself same question. Mb performance related? Imagine you have 100500 tiles of 2 types.
Anyways, here's how you can grab the props of a tile:
 

var map = game.add.tilemap('id-of-the-json-defined-in-the-game.load');// tile is a Phaser.Tile; i'm using x/y world coordinates to grab the tile, but you can also grab a tile by i/j indexes @see the docsvar tile = map.getTileWorldXY(x, y, undefined, undefined, 'your-layer-name-in-json');// null = not such tile under x/yif(tile != null){   // the tileset index      var index = map.getTilesetIndex('your-tileset-name-in-json');   // null = no such tileset   if(index != null)   {      var tileset = map.tilesets[index];      // finally you can grab the props of your tile - mind that Phaser.Tile.index starts with 1; however tileProperties start with 0;      var tileProps = tileset.tileProperties[tile.index-1];   }}

The really weird thing is why are tile's indexes and props indexes different -- a mystery

Link to comment
Share on other sites

The really weird thing is why are tile's indexes and props indexes different -- a mystery

 

I think that's a problem with the Tiled exporter, the properties don't follow the same "firstgid" rules that the other tiles do. Not sure why either.

 

I'm not sure performance is a reason to not copy the properties to the tiles, the tile objects already have a dozen or so properties, a few more can't really hurt. Plus because the properties are all in an object, all the tiles of that type would all point at the same object, which is already in memory against the tileset, so its not going to be a memory issue either. It probably just got forgotten.

 

I've already implemented my own version of  Phaser.TilemapParser.parseTiledJSON which copies the properties over to the tiles, and it works a treat. When I get a minute later i'm going to do a pull request on github and see if Rich wants it.

Link to comment
Share on other sites

Nah, i was wrong. Well, my code works as i've got one big tileset for all layers. But it won't work if you have many tilesets.
In case you have many tilesets the rules is simple:

tileset.firstgid + firstgid.tileProperties.id = tile.id

So to find the props you need:

/** * Return tile properties or null if not props are defined for this tile * @param map:Phaser.Tilemap    tilemap reference * @param tile:Phaser.Tile         the tile * @return Object                tile properties or null if no defined */var getTileProps(map, tile){    // grab the tile index    var tileIndex = tile.index;    // loop through all tilesets    for(var i = 0, n = map.tilesets.length; i < n; ++i)    {        var tileset = map.tilesets[i];        // prop index        var propIndex = tile.index - tileset.firstgid;        // prop index must be >=0, so the tile might be from this set        if(propIndex >= 0)        {            // properties found!            if(tileset.tileProperties[propIndex] != undefined)            {                return tileset.tileProperties[propIndex];            }        }    }    // no props found :-(    return null;}
Link to comment
Share on other sites

I'm not sure performance is a reason to not copy the properties to the tiles, the tile objects already have a dozen or so properties, a few more can't really hurt. Plus because the properties are all in an object, all the tiles of that type would all point at the same object, which is already in memory against the tileset, so its not going to be a memory issue either. It probably just got forgotten.

 

I've already implemented my own version of  Phaser.TilemapParser.parseTiledJSON which copies the properties over to the tiles, and it works a treat. When I get a minute later i'm going to do a pull request on github and see if Rich wants it.

 

Yeah, i was just about to recomend you rewrite the code http://docs.phaser.io/TilemapParser.js.html#sunlight-1-line-257

Link to comment
Share on other sites

Just started working with Phaser at the weekend and bumped into the same problem...

 

I think that's a problem with the Tiled exporter, the properties don't follow the same "firstgid" rules that the other tiles do. Not sure why either.

 

I'm not sure performance is a reason to not copy the properties to the tiles, the tile objects already have a dozen or so properties, a few more can't really hurt. Plus because the properties are all in an object, all the tiles of that type would all point at the same object, which is already in memory against the tileset, so its not going to be a memory issue either. It probably just got forgotten.

 

I've already implemented my own version of  Phaser.TilemapParser.parseTiledJSON which copies the properties over to the tiles, and it works a treat. When I get a minute later i'm going to do a pull request on github and see if Rich wants it.

Would love to see your implementation beeglebug.

Link to comment
Share on other sites

  • 2 weeks later...

Would love to see your implementation beeglebug.

 

Sorry it took so long, but I finally got round to putting in the pull request: https://github.com/photonstorm/phaser/pull/1126

 

Nothing fancy, basically the same code as above, but in the parseTiledJSON function. It lets you do nice short code like:

var tile = map.getTile(5,5);if(tile.properties.destructable) {    // do something}
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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