j0hnskot

Getting tile's properties.

Recommended Posts

I already did that before asking this question.The result of :

  var tile = map.getTile( 1,1, 'Tile Layer 1' );console.log( tile.properties);

is Object {} .

 

properties are empty. To be sure i entered properties to every tile , but still. Nothing on tile.properties.

 

But map.tilesets[0].tileProperties got every property set! 

Am i doing something wrong?

Share this post


Link to post
Share on other sites

I tried to make a map with only 1 layer and only 1 tile. This is the code 

var game = new Phaser.Game(640, 800, Phaser.CANVAS, '',  {preload: preload, create: create});function preload(){game.load.tilemap('map2', 'assets/maps/map2.json', null, Phaser.Tilemap.TILED_JSON);game.load.image('tiles', 'assets/grass.png');}var map;var layer;function create() {    game.stage.backgroundColor = '#787878';       map = game.add.tilemap('map2');    //  The first parameter is the tileset name, as specified in the Tiled map editor (and in the tilemap json file)    //  The second parameter maps this name to the Phaser.Cache key 'tiles'    map.addTilesetImage('grass', 'tiles');            layer = map.createLayer('Tile Layer 1');    //  This resizes the game world to match the layer dimensions    layer.resizeWorld();    var tile = map.getTile( 0, 0, 'Tile Layer 1' );console.log( tile.properties );

and this is the json :

{ "height":1, "layers":[        {         "data":[1],         "height":1,         "name":"Tile Layer 1",         "opacity":1,         "type":"tilelayer",         "visible":true,         "width":1,         "x":0,         "y":0        }], "orientation":"orthogonal", "properties":    {    }, "tileheight":32, "tilesets":[        {         "firstgid":1,         "image":"..\/grass.png",         "imageheight":32,         "imagewidth":32,         "margin":0,         "name":"grass",         "properties":            {            },         "spacing":0,         "tileheight":32,         "tileproperties":            {             "0":                {                 "1":"11"                }            },         "tilewidth":32        }], "tilewidth":32, "version":1, "width":1}

Do anyone see a mistake here? I really can't see what i'm doing wrong.

Share this post


Link to post
Share on other sites

Object layers (to be used with createFromObjects) are apparently different from tile layers (to be used with createLayer). In Tiled, as far as I can see, you cannot give a single tile on a layer its own properties, only to the master tile in the tileset, which is then instanced in the tile layer, and all instances have the same properties, stored in the tileset and not the layer. Objects on object layers however have per-instance properties which are automatically copied to the instances created by createFromObjects.

 

So the way to go is apparently to use the tileset. Highly impractical if you don't know which property you are looking for, but there it is :)

map.tilesets[setindex].tileProperties[tileindex]

Edit: tileindex, by the way, seems to be per-tileset and not equal to the tile's index property... so it's tile.index - tileset.firstgid or something. *sigh*

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Recently Browsing   0 members

    No registered users viewing this page.