Skip to content

Commit 14733e9

Browse files
committed
Tilemap.addTilesetImage can now accept a BitmapData as the key parameter and will use the BitmapData to render the tileset with instead of an image from the cache (thanks to @unstoppablecarl for the idea phaserjs#1838)
1 parent 30a3522 commit 14733e9

2 files changed

Lines changed: 26 additions & 23 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ Version 2.4 - "Katar" - in dev
350350
* The LoadTexture component has a new property `customRender` which is checked for in the Core postUpdate to know when to render custom elements like Videos.
351351
* BitmapText line spacing and word wrapping has been vastly improved and bought in-line with how Pixi 3 handles it, but with additional anchor support.
352352
* P2.Body.loadPolygon now allows the `key` parameter to be passed as `null` - when this happens the `object` parameter can be the actual physics object data instead of a string pointing to the cache, allowing you to take advantage of adding multiple convex shapes with automatic adjustments for center of mass #1801
353+
* Tilemap.addTilesetImage can now accept a BitmapData as the `key` parameter and will use the BitmapData to render the tileset with instead of an image from the cache (thanks to @unstoppablecarl for the idea #1838)
353354

354355
### Bug Fixes
355356

src/tilemap/Tilemap.js

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,9 @@ Phaser.Tilemap.prototype = {
243243
*
244244
* @method Phaser.Tilemap#addTilesetImage
245245
* @param {string} tileset - The name of the tileset as specified in the map data.
246-
* @param {string} [key] - The key of the Phaser.Cache image used for this tileset. If not specified it will look for an image with a key matching the tileset parameter.
246+
* @param {string|Phaser.BitmapData} [key] - The key of the Phaser.Cache image used for this tileset.
247+
* If `undefined` or `null` it will look for an image with a key matching the tileset parameter.
248+
* You can also pass in a BitmapData which can be used instead of an Image.
247249
* @param {number} [tileWidth=32] - The width of the tiles in the Tileset Image. If not given it will default to the map.tileWidth value, if that isn't set then 32.
248250
* @param {number} [tileHeight=32] - The height of the tiles in the Tileset Image. If not given it will default to the map.tileHeight value, if that isn't set then 32.
249251
* @param {number} [tileMargin=0] - The width of the tiles in the Tileset Image. If not given it will default to the map.tileWidth value.
@@ -253,6 +255,7 @@ Phaser.Tilemap.prototype = {
253255
*/
254256
addTilesetImage: function (tileset, key, tileWidth, tileHeight, tileMargin, tileSpacing, gid) {
255257

258+
if (typeof tileset === 'undefined') { return null; }
256259
if (typeof tileWidth === 'undefined') { tileWidth = this.tileWidth; }
257260
if (typeof tileHeight === 'undefined') { tileHeight = this.tileHeight; }
258261
if (typeof tileMargin === 'undefined') { tileMargin = 0; }
@@ -270,45 +273,44 @@ Phaser.Tilemap.prototype = {
270273
tileHeight = 32;
271274
}
272275

273-
if (typeof key === 'undefined')
276+
var img = null;
277+
278+
if (typeof key === 'undefined' || key === null)
274279
{
275-
if (typeof tileset === 'string')
276-
{
277-
key = tileset;
280+
key = tileset;
281+
}
278282

279-
if (!this.game.cache.checkImageKey(key))
280-
{
281-
console.warn('Phaser.Tilemap.addTilesetImage: Invalid image key given: "' + key + '"');
282-
return null;
283-
}
284-
}
285-
else
283+
if (key instanceof Phaser.BitmapData)
284+
{
285+
img = key.canvas;
286+
}
287+
else
288+
{
289+
if (!this.game.cache.checkImageKey(key))
286290
{
291+
console.warn('Phaser.Tilemap.addTilesetImage: Invalid image key given: "' + key + '"');
287292
return null;
288293
}
294+
295+
img = this.game.cache.getImage(key);
289296
}
290297

291-
if (typeof tileset === 'string')
298+
if (this.getTilesetIndex(tileset) === null && this.format === Phaser.Tilemap.TILED_JSON)
292299
{
293-
tileset = this.getTilesetIndex(tileset);
294-
295-
if (tileset === null && this.format === Phaser.Tilemap.TILED_JSON)
296-
{
297-
console.warn('Phaser.Tilemap.addTilesetImage: No data found in the JSON matching the tileset name: "' + key + '"');
298-
return null;
299-
}
300+
console.warn('Phaser.Tilemap.addTilesetImage: No data found in the JSON matching the tileset name: "' + key + '"');
301+
return null;
300302
}
301303

302304
if (this.tilesets[tileset])
303305
{
304-
this.tilesets[tileset].setImage(this.game.cache.getImage(key));
306+
this.tilesets[tileset].setImage(img);
305307
return this.tilesets[tileset];
306308
}
307309
else
308310
{
309-
var newSet = new Phaser.Tileset(key, gid, tileWidth, tileHeight, tileMargin, tileSpacing, {});
311+
var newSet = new Phaser.Tileset(tilset, gid, tileWidth, tileHeight, tileMargin, tileSpacing, {});
310312

311-
newSet.setImage(this.game.cache.getImage(key));
313+
newSet.setImage(img);
312314

313315
this.tilesets.push(newSet);
314316

0 commit comments

Comments
 (0)