Tiled object layer used for collide layer

Hi, I just created this obiect layer in Tiled and I’d like to use it as collide layer with player but I dont know how to get it. I was looking for some examples and tried something but I gave up after 5 hours.

phaser1

phaser2

Thanks for help.

Hi,
createFromObjects need more parameters:
https://photonstorm.github.io/phaser3-docs/Phaser.Tilemaps.Tilemap.html#createFromObjects__anchor

createFromObjects(name, id, spriteConfig [, scene])
Creates a Sprite for every object matching the given gid in the map data. All properties from the map data objectgroup are copied into the spriteConfig , so you can use this as an easy way to configure Sprite properties from within the map editor. For example giving an object a property of alpha: 0.5 in the map editor will duplicate that when the Sprite is created.

Look at this example, the coins are made with createFromObjects and a spritesheet:

OK thanks I got it but now how can I set collision on those objects ?

In the example if you make a:

var coins = map.createFromObjects('Coin Object Layer', 26, { key: 'coin' });
console.log(coins) // give an array of sprites

You get an array of sprites, so you can iterate this array and add each coin a body.

    coins.forEach(coin => {
        this.physics.world.enable(coin);
    })
    console.log(coins) // each coin has now a body

Now you can create a collider between a player and coins array

this.physics.add.collider(player, coins, null, null, this);
2 Likes

Thanks a lot I got it all. I hope.

@BlunT76 do we always need sprite gameobject type ? I tried with classType =>Rectangle, Zone, GameObject…but nothing works except with type sprite…

No, the doc says any class that extends GameObject. And there’s an example with Zone
classType: Phaser.GameObjects.Zone