Jump to content

Reading object layers from Tiled maps


tametick
 Share

Recommended Posts

Hi,

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

 

http://pastebin.com/p6jSrnQv

 

(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?

 

Thanks,

Ido.

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,

 

WeaveMN

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.

Guest
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.

Loading...
 Share

  • Recently Browsing   0 members

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