Get and remove a map tile by click

Hi all, I’m trying to get to grips with tile maps. I want to be able to create a map layer, then be able to get each tile and remove it by clicking on it.

I’ve created a simple map with one layer in Tiled Editor. I’ve then loaded the map into Phaser.

I was thinking somewhere along the lines of this, but I can’t get it to work.

this.input.on(‘pointerup’, function(pointer) {
var tile = map.getTileAt(pointer.x, pointer.y);
console.log(tile);
}, this);

But I keep getting “null” in the console.

Any help would be appreciated thanks.

Try

this.input.on('pointerup', function (pointer) {
  var tile = map.getTileAtWorldXY(pointer.worldX, pointer.worldY);

  console.log(pointer.worldX, pointer.worldY, tile);
}, this);

It works. Thanks a lot samme.

1 Like