Jump to content

How to get proper height and width from Tiled ObjectLayer


msqar
 Share

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:

{
         "draworder":"topdown",
         "name":"objects",
         "objects":[
                {
                 "height":10,
                 "id":1,
                 "name":"trap1",
                 "rotation":0,
                 "type":"trap",
                 "visible":true,
                 "width":128,
                 "x":445.41,
                 "y":627.62,
                 "gid": 2
                },
                {
                 "height":10,
                 "id":3,
                 "name":"trap2",
                 "rotation":0,
                 "type":"trap",
                 "visible":true,
                 "width":64,
                 "x":1023.87,
                 "y":627.54,
                 "gid": 2
                }],
         "opacity":1,
         "type":"objectgroup",
         "visible":true,
         "x":0,
         "y":0
        }],

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

traps = game.add.group();
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.

Link to comment
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`.

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

phaser-ce/pull/261

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.

Link to comment
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


map.objects.objects

 

Wow that worked... it was actually like this:

this.game.cache.getTilemapData(this.map.key).data.layers[1].objects

There i had the proper objects. 

But why is this happening?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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