Skip to content

Commit 1d13855

Browse files
committed
Tilemap.searchTileIndex allows you to search for the first tile matching the given index, with optional skip and reverse parameters.
1 parent 3f3655a commit 1d13855

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ Version 2.0.5 - "Tanchico" - in development
9797
* Mouse will now check if it's over the game canvas or not and set Pointer.withinGame accordingly.
9898
* Mouse.mouseOutCallback callback added for when the mouse is no longer over the game canvas.
9999
* Mouse.stopOnGameOut boolean controls if Pointer.stop will be called if the mouse leaves the game canvas (defaults to false)
100+
* Tilemap.searchTileIndex allows you to search for the first tile matching the given index, with optional skip and reverse parameters.
100101

101102

102103
### New Plugins

src/tilemap/Tilemap.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,73 @@ Phaser.Tilemap.prototype = {
12081208

12091209
},
12101210

1211+
/**
1212+
* Searches the entire map layer for the first tile matching the given index, then returns that Phaser.Tile object.
1213+
* If no match is found it returns null.
1214+
* The search starts from the top-left tile and continues horizontally until it hits the end of the row, then it drops down to the next column.
1215+
* If the reverse boolean is true, it scans starting from the bottom-right corner travelling up to the top-left.
1216+
*
1217+
* @method Phaser.Tilemap#searchTileIndex
1218+
* @param {number} index - The tile index value to search for.
1219+
* @param {number} [skip=0] - The number of times to skip a matching tile before returning.
1220+
* @param {number} [reverse=false] - If true it will scan the layer in reverse, starting at the bottom-right. Otherwise it scans from the top-left.
1221+
* @param {number|string|Phaser.TilemapLayer} [layer] - The layer to get the tile from.
1222+
* @return {Phaser.Tile} The first (or n skipped) tile with the matching index.
1223+
*/
1224+
searchTileIndex: function (index, skip, reverse, layer) {
1225+
1226+
if (typeof skip === 'undefined') { skip = 0; }
1227+
if (typeof reverse === 'undefined') { reverse = false; }
1228+
1229+
layer = this.getLayer(layer);
1230+
1231+
var c = 0;
1232+
1233+
if (reverse)
1234+
{
1235+
for (var y = this.layers[layer].height - 1; y >= 0; y--)
1236+
{
1237+
for (var x = this.layers[layer].width - 1; x >= 0; x--)
1238+
{
1239+
if (this.layers[layer].data[y][x].index === index)
1240+
{
1241+
if (c === skip)
1242+
{
1243+
return this.layers[layer].data[y][x];
1244+
}
1245+
else
1246+
{
1247+
c++;
1248+
}
1249+
}
1250+
}
1251+
}
1252+
}
1253+
else
1254+
{
1255+
for (var y = 0; y < this.layers[layer].height; y++)
1256+
{
1257+
for (var x = 0; x < this.layers[layer].width; x++)
1258+
{
1259+
if (this.layers[layer].data[y][x].index === index)
1260+
{
1261+
if (c === skip)
1262+
{
1263+
return this.layers[layer].data[y][x];
1264+
}
1265+
else
1266+
{
1267+
c++;
1268+
}
1269+
}
1270+
}
1271+
}
1272+
}
1273+
1274+
return null;
1275+
1276+
},
1277+
12111278
/**
12121279
* Gets a tile from the Tilemap Layer. The coordinates are given in tile values.
12131280
*

0 commit comments

Comments
 (0)