Skip to content

Commit 197fef7

Browse files
committed
Tilemap.removeTile is a new method that allows you to remove a tile, or an array of tiles, by passing in references to the tiles themselves, rather than coordinates. The tiles can be replaced with new tiles of the given index, or removed entirely, and the method can optionally recalculate interesting faces on the layer.
1 parent 8167d6d commit 197fef7

1 file changed

Lines changed: 43 additions & 1 deletion

File tree

src/tilemaps/Tilemap.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1566,6 +1566,48 @@ var Tilemap = new Class({
15661566
return this;
15671567
},
15681568

1569+
/**
1570+
* Removes the given Tile, or an array of Tiles, from the layer to which they belong,
1571+
* and optionally recalculates the collision information.
1572+
*
1573+
* This cannot be applied to Tiles that belong to Static Tilemap Layers.
1574+
*
1575+
* @method Phaser.Tilemaps.Tilemap#removeTile
1576+
* @since 3.16.3
1577+
*
1578+
* @param {(Phaser.Tilemaps.Tile|Phaser.Tilemaps.Tile[])} tiles - The Tile to remove, or an array of Tiles.
1579+
* @param {integer} [replaceIndex=-1] - After removing the Tile, insert a brand new Tile into its location with the given index. Leave as -1 to just remove the tile.
1580+
* @param {boolean} [recalculateFaces=true] - `true` if the faces data should be recalculated.
1581+
*
1582+
* @return {Phaser.Tilemaps.Tile[]} Returns an array of Tiles that were removed.
1583+
*/
1584+
removeTile: function (tiles, replaceIndex, recalculateFaces)
1585+
{
1586+
if (replaceIndex === undefined) { replaceIndex = -1; }
1587+
if (recalculateFaces === undefined) { recalculateFaces = true; }
1588+
1589+
var removed = [];
1590+
1591+
if (!Array.isArray(tiles))
1592+
{
1593+
tiles = [ tiles ];
1594+
}
1595+
1596+
for (var i = 0; i < tiles.length; i++)
1597+
{
1598+
var tile = tiles[i];
1599+
1600+
removed.push(this.removeTileAt(tile.x, tile.y, true, recalculateFaces, tile.tilemapLayer));
1601+
1602+
if (replaceIndex > -1)
1603+
{
1604+
this.putTileAt(replaceIndex, tile.x, tile.y, recalculateFaces, tile.tilemapLayer);
1605+
}
1606+
}
1607+
1608+
return removed;
1609+
},
1610+
15691611
/**
15701612
* Removes the tile at the given tile coordinates in the specified layer and updates the layer's
15711613
* collision information.
@@ -1582,7 +1624,7 @@ var Tilemap = new Class({
15821624
* @param {boolean} [recalculateFaces=true] - `true` if the faces data should be recalculated.
15831625
* @param {(string|integer|Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer)} [layer] - The tile layer to use. If not given the current layer is used.
15841626
*
1585-
* @return {?Phaser.Tilemaps.Tile} Returns a Tile, or null if the layer given was invalid.
1627+
* @return {?Phaser.Tilemaps.Tile} Returns the Tile that was removed, or null if the layer given was invalid.
15861628
*/
15871629
removeTileAt: function (tileX, tileY, replaceWithNull, recalculateFaces, layer)
15881630
{

0 commit comments

Comments
 (0)