Skip to content

Commit 38d722c

Browse files
committed
Tilemap.removeTile(x, y, layer) lets you remove the tile at the given coordinates and updates the collision data.
Tilemap.removeTileWorldXY lets you remove the tile at the given pixel value coordinates and updates the collision data. If you pass `null` to Tilemap.putTile as the tile parameter it will pass the call over to Tilemap.removeTile.
1 parent 61429d8 commit 38d722c

2 files changed

Lines changed: 65 additions & 2 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ Updated
7474
* The Phaser jshint process is now running on Travis (thanks @xtian, #656)
7575
* The Phaser Gruntfile is now split up into option tasks (thanks @xtian, #638)
7676
* Key.reset now clears any callbacks associated with the onDown and onUp events and nulls the onHoldCallback if set. Key.reset is called by Keyboard.reset when changing state.
77+
* If you pass `null` to Tilemap.putTile as the tile parameter it will pass the call over to Tilemap.removeTile.
7778

7879

7980
New Features
@@ -92,6 +93,8 @@ New Features
9293
* SoundManager.removeByKey(key) will remove all sounds from the SoundManager that have a key matching the given value.
9394
* ArcadePhysics.Body.hitTest(x, y) will return a boolean based on if the given world coordinate are within the Body or not.
9495
* StateManager.restart allows you to quickly restart the *current* state, optionally clearing the world and cache.
96+
* Tilemap.removeTile(x, y, layer) lets you remove the tile at the given coordinates and updates the collision data.
97+
* Tilemap.removeTileWorldXY lets you remove the tile at the given pixel value coordinates and updates the collision data.
9598

9699

97100
Bug Fixes

src/tilemap/Tilemap.js

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,18 +1000,77 @@ Phaser.Tilemap.prototype = {
10001000

10011001
},
10021002

1003+
/**
1004+
* Removes the tile located at the given coordinates and updates the collision data.
1005+
*
1006+
* @method Phaser.Tilemap#removeTile
1007+
* @param {number} x - X position to place the tile (given in tile units, not pixels)
1008+
* @param {number} y - Y position to place the tile (given in tile units, not pixels)
1009+
* @param {number|string|Phaser.TilemapLayer} [layer] - The layer to modify.
1010+
* @return {Phaser.Tile} The Tile object that was removed from this map.
1011+
*/
1012+
removeTile: function (x, y, layer) {
1013+
1014+
layer = this.getLayer(layer);
1015+
1016+
if (x >= 0 && x < this.layers[layer].width && y >= 0 && y < this.layers[layer].height)
1017+
{
1018+
if (this.hasTile(x, y, layer))
1019+
{
1020+
var tile = this.layers[layer].data[y][x];
1021+
1022+
this.layers[layer].data[y][x] = null;
1023+
1024+
this.layers[layer].dirty = true;
1025+
1026+
this.calculateFaces(layer);
1027+
1028+
return tile;
1029+
}
1030+
}
1031+
1032+
},
1033+
1034+
/**
1035+
* Removes the tile located at the given coordinates and updates the collision data. The coordinates are given in pixel values.
1036+
*
1037+
* @method Phaser.Tilemap#removeTileWorldXY
1038+
* @param {number} x - X position to insert the tile (given in pixels)
1039+
* @param {number} y - Y position to insert the tile (given in pixels)
1040+
* @param {number} tileWidth - The width of the tile in pixels.
1041+
* @param {number} tileHeight - The height of the tile in pixels.
1042+
* @param {number|string|Phaser.TilemapLayer} [layer] - The layer to modify.
1043+
* @return {Phaser.Tile} The Tile object that was removed from this map.
1044+
*/
1045+
removeTileWorldXY: function (x, y, tileWidth, tileHeight, layer) {
1046+
1047+
layer = this.getLayer(layer);
1048+
1049+
x = this.game.math.snapToFloor(x, tileWidth) / tileWidth;
1050+
y = this.game.math.snapToFloor(y, tileHeight) / tileHeight;
1051+
1052+
return this.removeTile(x, y, layer);
1053+
1054+
},
1055+
10031056
/**
10041057
* Puts a tile of the given index value at the coordinate specified.
1058+
* If you pass `null` as the tile it will pass your call over to Tilemap.removeTile instead.
10051059
*
10061060
* @method Phaser.Tilemap#putTile
1007-
* @param {Phaser.Tile|number} tile - The index of this tile to set or a Phaser.Tile object.
1061+
* @param {Phaser.Tile|number|null} tile - The index of this tile to set or a Phaser.Tile object. If null the tile is removed from the map.
10081062
* @param {number} x - X position to place the tile (given in tile units, not pixels)
10091063
* @param {number} y - Y position to place the tile (given in tile units, not pixels)
10101064
* @param {number|string|Phaser.TilemapLayer} [layer] - The layer to modify.
10111065
* @return {Phaser.Tile} The Tile object that was created or added to this map.
10121066
*/
10131067
putTile: function (tile, x, y, layer) {
10141068

1069+
if (tile === null)
1070+
{
1071+
return this.removeTile(x, y, layer);
1072+
}
1073+
10151074
layer = this.getLayer(layer);
10161075

10171076
if (x >= 0 && x < this.layers[layer].width && y >= 0 && y < this.layers[layer].height)
@@ -1075,6 +1134,7 @@ Phaser.Tilemap.prototype = {
10751134
* @param {number} tileWidth - The width of the tile in pixels.
10761135
* @param {number} tileHeight - The height of the tile in pixels.
10771136
* @param {number|string|Phaser.TilemapLayer} [layer] - The layer to modify.
1137+
* @return {Phaser.Tile} The Tile object that was created or added to this map.
10781138
*/
10791139
putTileWorldXY: function (tile, x, y, tileWidth, tileHeight, layer) {
10801140

@@ -1083,7 +1143,7 @@ Phaser.Tilemap.prototype = {
10831143
x = this.game.math.snapToFloor(x, tileWidth) / tileWidth;
10841144
y = this.game.math.snapToFloor(y, tileHeight) / tileHeight;
10851145

1086-
this.putTile(tile, x, y, layer);
1146+
return this.putTile(tile, x, y, layer);
10871147

10881148
},
10891149

0 commit comments

Comments
 (0)