Run function on tile overlap based on parameter

I have a tilemap where som tiles are “lava”. I want to run my “kill player” function on overlap with these tiles.

In the map i plan to have lava, spikes and other tiles that kill the player so i have given all these tiles a parameter (in the Tiles editor). Now i want to check overlap with player and the tiles with the “kill” parameter and run my function.

Im thinking the setTileIndexCallback method is the way to go but it wants a tile ID. There may me a way to get the ID of all tiles with the specified parameter?

You can do something like

tilemapLayer.getTilesWithin() // all tiles
  .filter(function (tile) { 
    return tile.properties.isLava;
  });
1 Like

Thanks samme!

I need to get better at looking in the prototypes. This looks like it’ll get me over the biggest hurdle.

tilemapLayer.filterTiles(function (tile) { 
  return tile.properties.isLava;
}); // → Array.<Tile>
1 Like

Thanks. Thats a little cleaner.

I ended up with the following:

const deadlyTiles = this.foreground.filterTiles(tile => tile.properties.deadly).map(x => x.index);
const deadlyTilesId = [...(new Set(deadlyTiles))];

this.foreground.setTileIndexCallback(deadlyTilesId, this.killPlayer, this);
this.physics.add.overlap(this.player, this.foreground);