Sign in to follow this  

How to get proper height and width from Tiled ObjectLayer

Recommended Posts

Hi people, i'm new into Phaser and its community.

I made a little test map with an Object Layer in it, the idea is to add some traps sectors or teleports where if the player touches it, it will teletransport it to some random place. Hence, it won't add any texture to it, i just want to have that area to collide with my player. Is this possible? without having to attach a texture to it?

I used the square polygon to do so on Tiled Maps. Exported it as JSON. So here's my Object JSON:

                 "gid": 2
                 "gid": 2

Then i created a group of traps and try to iterate them one by one to avoid them from falling due to gravity:

traps =;
traps.enableBody = true;

// Get all traps
map.createFromObjects('objects', 2, null, 0, true, false, traps);

traps.forEach(function(trap) {
    trap.body.immovable = true;
    trap.body.allowGravity = false;

I get the trap individual objects properly, but the problem that i found is, i'm not able to get the width/height i set on Tiled or shows up on my JSON. They are both equals to 1.

Why did i lose those width & height values? but others don't such as `x` and `y` coordinates.

What do you recommend to do to achieve the same effect? I just want certain areas to be "readable" so i can call functions after collision.

It works fine when i set the texture, but also sets the default height x width of the texture applied, and i don't have any way to get the real Tiled values so i can resize my texture properly.

Right now if I add a texture it works with this:

this.physics.arcade.overlap(player.character, traps, this.resetPlayer, null, this);


Even if i try to get the objects by doing: 

var trap = map.objects['objects'][1]

The object returned doesn't contain any width/height. This is what it's returning:

  1. gid:2
  2. name:"trap2"
  3. properties:undefined
  4. type:""
  5. visible:true
  6. x:1023.87
  7. y:627.54

I don't know if i explained myself correctly. Hope someone helps me :D

Thanks in advance.

Share this post

Link to post
Share on other sites

The only way i seemed to get the width and height is by adding them inside properties from Tiled or from JSON itself manually. Replicating the same values that were put in the root object. Is that normal? that way the object/entity size was correct. Seemed to be reading fine the width and height, but the "original" ones are still equals to `1`.

Share this post

Link to post
Share on other sites
21 hours ago, samme said:

I think the latest Phaser CE automatically assigns width and height from the object. Some earlier Phaser versions ignored them and instead set width and height from the texture key passed in createFromObjects.


So it means i'm using an old Phaser version? i downloaded the latest. And indeed it's ignoring those width/height properties from the ObjectLayer. I had to manually put them inside "Custom Properties" and that way it worked.

Share this post

Link to post
Share on other sites
1 hour ago, samme said:

Double-check the map data

game.cache.getTilemapData(map.key).data.layers.objects // I think

and the object data



Wow that worked... it was actually like this:[1].objects

There i had the proper objects. 

But why is this happening?

Share this post

Link to post
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.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.