Jump to content

Reading object layers from Tiled maps


Recommended Posts


I'm using phaser's Tilemap to read the following map from Tiled:




(this is what it looks like: http://i.imgur.com/3bp6JFg.png)


Now, although the first layer of tiles is read properly, the object layer that comes after it doesn't seem to get read at all. 


I have this in the preloader:

game.load.tilemap('level', 'assets/level0.json', null, Tilemap.TILED_JSON);


and later:

var tileMap = Main.game.add.tilemap('level');


when i try to log tileMap.layers it only shows one layer (the first one). Any idea how to get the object layer?




Link to comment
Share on other sites

Nice, I was just working on something like this. My idea is to have an Entity registry and to be able to create entities based on the object properties, so I don't have to create all the objects by hand.


@rich where I can see this so I see how it's implemented and I don't do the same again?

Link to comment
Share on other sites

my 'nadion' phaser add-on/library (https://github.com/jcd-as/nadion) uses a method like this to load objects. no registry, it simply looks for the object first in the Nadion namespace, then in the namespace the object gives (if any) and lastly in the global namespace.


(nadion is some share-able bits pulled from the project I'm working on, mainly around using Tiled to design levels)


looks like I may need to amend it to work with 1.1.4 :)

Link to comment
Share on other sites

If you can't wait for 1.1.4 to read the object layer data I whipped up this simple function leveraging jQuery.each() and a switch statement. 

function addEntities(){    //get the raw map JSON so we can access the object layer    mapData = game.cache.getTilemapData('game1');    monsters = game.add.group();    $.each(mapData.data.layers, function(index, layer){        if(layer.type == 'objectgroup')        {            $.each(layer.objects, function(index, obj){                switch(obj.name)                {                    case 'player':                        sprite = game.add.sprite(obj.x, obj.y, 'guy');                        sprite.anchor.setTo(0.5, 0.5);                        game.camera.follow(sprite);                        break;                    case 'monster':                        var monsterNum = Math.floor((Math.random()*2)+1);                        monsters.create(obj.x, obj.y, 'monster'+monsterNum)                        break;                    case 'exit':                        break;                }            });        }    });}

Hope that helps,



Link to comment
Share on other sites


  • Recently Browsing   0 members

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