Detecting Tiled Layer Overlap

Hey Phaser amigos. I have a very specific case and maybe you could help me understand what am I doing wrong.

I have this layer with ropes:

It has been created this way:

game.ropeTiles = game.map.addTilesetImage('tiles');
game.ropeLayer = game.map.createDynamicLayer('Ropes', game.ropeTiles, 0, 0);

What I’m trying to do is to know when the player overlaps with this layer so I can hang the monkey to the rope.

I tried something like this:

game.physics.add.overlap(game.player, game.ropeLayer, function(thePlayer, tile) {
	console.log('overlaping:', tile);
});

But it’s constantly returning the console log, not just when overlaping.

So two questions:

  1. Overlapping with Layers works this way?
  2. Should I create my ropes as another type of instance? maybe objects?

Thanks a lot friends, have a great day!

1 Like

I think the problem is, that the rope layer (like any tilemap layer) is filling the whole screen (not just where the ropes are visible). Because the layer is still there (just transparent).

You could check which tile is at the position of the monkeys hands on the tilemap layer and make the decision that way:

var tile = ropeLayer.getTileAtWorldXY(monkey.x, monkey.y);
if (tile.index == 1) {
  monkey.hang();
}

With index beeing the index in the tileset of the layer.

3 Likes

This makes a lot of sence @jppresents. Tried this approach and it’s working.
Thanks a lot for your help :slight_smile:

1 Like