Skip to content

Commit a2b7d33

Browse files
committed
Tilemap.createFromTiles will convert all tiles matching the given tile index (or an array of indexes) into Sprites. You can optionally then replace these tiles if you wish. This is perfect for games when you want to turn specific tiles into Sprites for extra control. The Sprites have an optional properties object which they can be populated with.
1 parent 3479786 commit a2b7d33

2 files changed

Lines changed: 116 additions & 18 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ Version 2.2.0 - "Bethal" - in development
105105
* Time.slowMotion allows you to push the game into a slow motion mode. The default value is 1.0. 2.0 would be half speed, and so on.
106106
* Time.timeCap is no longer used and now deprecated. All timing is now handled by the fixed time-step code we've introduced.
107107
* Time.now can no longer be relied upon to contain a timestamp value. If the browser supports requestAnimationFrame then `Time.now` will contain the high resolution timer value that rAf generates. Otherwise it will contain the value of Date.now. If you require the actual time value (in milliseconds) then please use `Time.time` instead. Note that all Phaser sub-systems that used to rely on `Time.now` have been updated, so if you have any code that extends these please be sure to check it.
108+
* Tilemap.createFromTiles will convert all tiles matching the given tile index (or an array of indexes) into Sprites. You can optionally then replace these tiles if you wish. This is perfect for games when you want to turn specific tiles into Sprites for extra control. The Sprites have an optional properties object which they can be populated with.
108109

109110
### Updates
110111

src/tilemap/Tilemap.js

Lines changed: 115 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,100 @@ Phaser.Tilemap.prototype = {
418418

419419
},
420420

421+
/**
422+
* Creates a Sprite for every object matching the given tile indexes in the map data.
423+
* You can specify the group that the Sprite will be created in. If none is given it will be created in the World.
424+
* You can optional specify if the tile will be replaced with another after the Sprite is created. This is useful if you want to lay down special
425+
* tiles in a level that are converted to Sprites, but want to replace the tile itself with a floor tile or similar once converted.
426+
*
427+
* @method Phaser.Tilemap#createFromTiles
428+
* @param {integer|Array} tiles - The tile index, or array of indexes, to create Sprites from.
429+
* @param {integer|Array} replacements - The tile index, or array of indexes, to change a converted tile to. Set to `null` to not change.
430+
* @param {string} key - The Game.cache key of the image that this Sprite will use.
431+
* @param {Phaser.Group} [group=Phaser.World] - Group to add the Sprite to. If not specified it will be added to the World group.
432+
* @param {object} [properties] - An object that contains the default properties for your newly created Sprite. This object will be iterated and any matching Sprite property will be set.
433+
* @param {number|string|Phaser.TilemapLayer} [layer] - The layer to operate on.
434+
* @return {integer} The number of Sprites that were created.
435+
*/
436+
createFromTiles: function (tiles, replacements, key, layer, group, properties) {
437+
438+
if (typeof tiles === 'number') { tiles = [tiles]; }
439+
440+
if (typeof replacements === 'undefined' || replacements === null)
441+
{
442+
replacements = [];
443+
}
444+
else if (typeof replacements === 'number')
445+
{
446+
replacements = [replacements];
447+
}
448+
449+
layer = this.getLayer(layer);
450+
451+
if (typeof group === 'undefined') { group = this.game.world; }
452+
if (typeof properties === 'undefined') { properties = {} };
453+
454+
if (properties.customClass === undefined)
455+
{
456+
properties.customClass = Phaser.Sprite;
457+
}
458+
459+
if (properties.adjustY === undefined)
460+
{
461+
properties.adjustY = true;
462+
}
463+
464+
var lw = this.layers[layer].width;
465+
var lh = this.layers[layer].height;
466+
467+
this.copy(0, 0, lw, lh, layer);
468+
469+
if (this._results.length < 2)
470+
{
471+
return 0;
472+
}
473+
474+
var total = 0;
475+
var sprite;
476+
477+
for (var i = 1, len = this._results.length; i < len; i++)
478+
{
479+
if (tiles.indexOf(this._results[i].index) !== -1)
480+
{
481+
sprite = new properties.customClass(this.game, this._results[i].worldX, this._results[i].worldY, key);
482+
483+
for (var property in properties)
484+
{
485+
sprite[property] = properties[property];
486+
}
487+
488+
group.add(sprite);
489+
total++;
490+
}
491+
492+
}
493+
494+
if (replacements.length === 1)
495+
{
496+
// Assume 1 replacement for all types of tile given
497+
for (i = 0; i < tiles.length; i++)
498+
{
499+
this.replace(tiles[i], replacements[0], 0, 0, lw, lh, layer);
500+
}
501+
}
502+
else if (replacements.length > 1)
503+
{
504+
// Assume 1 for 1 mapping
505+
for (i = 0; i < tiles.length; i++)
506+
{
507+
this.replace(tiles[i], replacements[i], 0, 0, lw, lh, layer);
508+
}
509+
}
510+
511+
return total;
512+
513+
},
514+
421515
/**
422516
* Creates a new TilemapLayer object. By default TilemapLayers are fixed to the camera.
423517
* The `layer` parameter is important. If you've created your map in Tiled then you can get this by looking in Tiled and looking at the Layer name.
@@ -866,10 +960,6 @@ Phaser.Tilemap.prototype = {
866960
{
867961
layer = this.currentLayer;
868962
}
869-
// else if (typeof layer === 'number')
870-
// {
871-
// layer = layer;
872-
// }
873963
else if (typeof layer === 'string')
874964
{
875965
layer = this.getLayerIndex(layer);
@@ -884,25 +974,32 @@ Phaser.Tilemap.prototype = {
884974
},
885975

886976
/**
887-
* Turn off/on the recalculation of faces for tile or collission updates.
888-
* setPreventRecalculate(true) puts recalculation on hold while
889-
* setPreventRecalculate(false) recalculates all the changed layers.
977+
* Turn off/on the recalculation of faces for tile or collision updates.
978+
* `setPreventRecalculate(true)` puts recalculation on hold while `setPreventRecalculate(false)` recalculates all the changed layers.
890979
*
891980
* @method Phaser.Tilemap#setPreventRecalculate
892-
* @param {boolean} if true it will put the recalculation on hold.
981+
* @param {boolean} value - If true it will put the recalculation on hold.
893982
*/
894983
setPreventRecalculate: function (value) {
895-
if((value===true)&&(this.preventingRecalculate!==true)){
984+
985+
if (value === true && this.preventingRecalculate !== true)
986+
{
896987
this.preventingRecalculate = true;
897988
this.needToRecalculate = {};
898989
}
899-
if((value===false)&&(this.preventingRecalculate===true)){
990+
991+
if (value === false && this.preventingRecalculate === true)
992+
{
900993
this.preventingRecalculate = false;
901-
for(var i in this.needToRecalculate){
994+
995+
for (var i in this.needToRecalculate)
996+
{
902997
this.calculateFaces(i);
903998
}
999+
9041000
this.needToRecalculate = false;
9051001
}
1002+
9061003
},
9071004

9081005
/**
@@ -1373,14 +1470,14 @@ Phaser.Tilemap.prototype = {
13731470
* Copies all of the tiles in the given rectangular block into the tilemap data buffer.
13741471
*
13751472
* @method Phaser.Tilemap#copy
1376-
* @param {number} x - X position of the top left of the area to copy (given in tiles, not pixels)
1377-
* @param {number} y - Y position of the top left of the area to copy (given in tiles, not pixels)
1378-
* @param {number} width - The width of the area to copy (given in tiles, not pixels)
1379-
* @param {number} height - The height of the area to copy (given in tiles, not pixels)
1380-
* @param {number|string|Phaser.TilemapLayer} [layer] - The layer to copy the tiles from.
1473+
* @param {integer} x - X position of the top left of the area to copy (given in tiles, not pixels)
1474+
* @param {integer} y - Y position of the top left of the area to copy (given in tiles, not pixels)
1475+
* @param {integer} width - The width of the area to copy (given in tiles, not pixels)
1476+
* @param {integer} height - The height of the area to copy (given in tiles, not pixels)
1477+
* @param {integer|string|Phaser.TilemapLayer} [layer] - The layer to copy the tiles from.
13811478
* @return {array} An array of the tiles that were copied.
13821479
*/
1383-
copy: function (x, y, width, height, layer) {
1480+
copy: function (x, y, width, height, layer, indexes) {
13841481

13851482
layer = this.getLayer(layer);
13861483

@@ -1394,7 +1491,7 @@ Phaser.Tilemap.prototype = {
13941491
if (typeof y === "undefined") { y = 0; }
13951492
if (typeof width === "undefined") { width = this.layers[layer].width; }
13961493
if (typeof height === "undefined") { height = this.layers[layer].height; }
1397-
1494+
13981495
if (x < 0)
13991496
{
14001497
x = 0;

0 commit comments

Comments
 (0)