Skip to content

Commit ff7cad1

Browse files
committed
All of the setCollision methods now have a 'collides' parameter so you can enable or disable collision, not only enable it. Also fixed Loader example.
1 parent 8479e45 commit ff7cad1

2 files changed

Lines changed: 115 additions & 76 deletions

File tree

examples/loader/load tilemap json.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: p
33

44
function preload() {
55

6-
76
// Tilemaps are split into two parts: The actual map data (usually stored in a CSV or JSON file)
87
// and the tileset/s used to render the map.
98

@@ -16,27 +15,33 @@ function preload() {
1615
// The final one tells Phaser the foramt of the map data, in this case it's a JSON file exported from the Tiled map editor.
1716
// This could be Phaser.Tilemap.CSV too.
1817

19-
game.load.tilemap('mario', 'assets/maps/mario1.json', null, Phaser.Tilemap.TILED_JSON);
18+
game.load.tilemap('mario', 'assets/tilemaps/maps/super_mario.json', null, Phaser.Tilemap.TILED_JSON);
2019

21-
// Next we load the tileset. This consists of an image and a set of values that determine the size of the tiles within the image.
22-
// In this case we give it a unique key, the URL to the PNG file and tell Phaser the tiles are all 16x16 pixels in size.
20+
// Next we load the tileset. This is just an image, loaded in via the normal way we load images:
2321

24-
game.load.tileset('tiles', 'assets/maps/mario1.png', 16, 16);
22+
game.load.image('tiles', 'assets/tilemaps/tiles/super_mario.png');
2523

2624
}
2725

2826
var map;
29-
var tileset;
3027
var layer;
3128

3229
function create() {
3330

3431
game.stage.backgroundColor = '#787878';
3532

33+
// The 'mario' key here is the Loader key given in game.load.tilemap
3634
map = game.add.tilemap('mario');
35+
36+
// The first parameter is the tileset name, as specified in the Tiled map editor (and in the tilemap json file)
37+
// The second parameter maps this name to the Phaser.Cache key 'tiles'
38+
map.addTilesetImage('SuperMarioBros-World1-1', 'tiles');
3739

38-
tileset = game.add.tileset('tiles');
40+
// Creates a layer from the World1 layer in the map data.
41+
// A Layer is effectively like a Phaser.Sprite, so is added to the display list.
42+
layer = map.createLayer('World1');
3943

40-
layer = game.add.tilemapLayer(0, 0, 800, 600, tileset, map, 0);
44+
// This resizes the game world to match the layer dimensions
45+
layer.resizeWorld();
4146

4247
}

src/tilemap/Tilemap.js

Lines changed: 102 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -355,59 +355,72 @@ Phaser.Tilemap.prototype = {
355355

356356
return this.getIndex(this.objects, name);
357357

358+
},
359+
360+
setTileCallback: function (indexes, layer, callback, callbackContext) {
361+
362+
363+
358364
},
359365

360366
/**
361-
* Sets collision on all tiles in the given layer, except for the IDs of those in the given array.
367+
* Sets collision the given tile or tiles. You can pass in either a single numeric index or an array of indexes: [ 2, 3, 15, 20].
368+
* The `collides` parameter controls if collision will be enabled (true) or disabled (false).
362369
*
363-
* @method Phaser.Tileset#setCollisionByExclusion
364-
* @param {array} indexes - An array of the tile IDs to not be counted for collision.
365-
* @param {number|string|Phaser.TilemapLayer} layer - The layer to operate on. If not given will default to this.currentLayer.
370+
* @method Phaser.Tileset#setCollision
371+
* @param {number|array} indexes - Either a single tile index, or an array of tile IDs to be checked for collision.
372+
* @param {boolean} [collides=true] - If true it will enable collision. If false it will clear collision.
373+
* @param {number|string|Phaser.TilemapLayer} [layer] - The layer to operate on. If not given will default to this.currentLayer.
366374
*/
367-
setCollisionByExclusion: function (indexes, layer) {
375+
setCollision: function (indexes, collides, layer) {
368376

369-
if (typeof layer === 'undefined')
377+
if (typeof collides === 'undefined') { collides = true; }
378+
379+
layer = this.getLayer(layer);
380+
381+
if (typeof indexes === 'number')
370382
{
371-
layer = this.currentLayer;
383+
return this.setCollisionByIndex(indexes, collides, layer, true);
372384
}
373-
374-
// Collide everything, except the IDs given in the indexes array
375-
for (var i = 0, len = this.tiles.length; i < len; i++)
385+
else
376386
{
377-
if (indexes.indexOf(i) === -1)
387+
// Collide all of the IDs given in the indexes array
388+
for (var i = 0, len = indexes.length; i < len; i++)
378389
{
379-
this.setCollisionByIndex(i, layer, false);
390+
this.setCollisionByIndex(indexes[i], collides, layer, false);
380391
}
381-
}
382392

383-
// Now re-calculate interesting faces
384-
this.calculateFaces(layer);
393+
// Now re-calculate interesting faces
394+
this.calculateFaces(layer);
395+
}
385396

386397
},
387398

388399
/**
389-
* Sets collision the given tile index, or array of tiles indexes.
400+
* Sets collision on a range of tiles where the tile IDs increment sequentially.
401+
* Calling this with a start value of 10 and a stop value of 14 would set collision for tiles 10, 11, 12, 13 and 14.
402+
* The `collides` parameter controls if collision will be enabled (true) or disabled (false).
390403
*
391-
* @method Phaser.Tileset#setCollision
392-
* @param {number|array} indexes - Either a single tile index, or an array of tile IDs to be checked for collision.
393-
* @param {number|string|Phaser.TilemapLayer} layer - The layer to operate on. If not given will default to this.currentLayer.
404+
* @method Phaser.Tileset#setCollisionBetween
405+
* @param {number} start - The first index of the tile to be set for collision.
406+
* @param {number} stop - The last index of the tile to be set for collision.
407+
* @param {boolean} [collides=true] - If true it will enable collision. If false it will clear collision.
408+
* @param {number|string|Phaser.TilemapLayer} [layer] - The layer to operate on. If not given will default to this.currentLayer.
394409
*/
395-
setCollision: function (indexes, layer) {
410+
setCollisionBetween: function (start, stop, collides, layer) {
396411

397-
if (typeof layer === 'undefined')
398-
{
399-
layer = this.currentLayer;
400-
}
412+
if (typeof collides === 'undefined') { collides = true; }
401413

402-
if (typeof indexes === 'number')
414+
layer = this.getLayer(layer);
415+
416+
if (start > stop)
403417
{
404-
return this.setCollisionByIndex(indexes, layer);
418+
return;
405419
}
406420

407-
// Collide all of the IDs given in the indexes array
408-
for (var i = 0, len = indexes.length; i < len; i++)
421+
for (var index = start; index <= stop; index++)
409422
{
410-
this.setCollisionByIndex(indexes[i], layer, false);
423+
this.setCollisionByIndex(index, collides, layer, false);
411424
}
412425

413426
// Now re-calculate interesting faces
@@ -416,54 +429,51 @@ Phaser.Tilemap.prototype = {
416429
},
417430

418431
/**
419-
* Sets collision on a range of tiles.
432+
* Sets collision on all tiles in the given layer, except for the IDs of those in the given array.
433+
* The `collides` parameter controls if collision will be enabled (true) or disabled (false).
420434
*
421-
* @method Phaser.Tileset#setCollisionBetween
422-
* @param {number} start - The first index of the tile to be set for collision.
423-
* @param {number} stop - The last index of the tile to be set for collision.
424-
* @param {number|string|Phaser.TilemapLayer} layer - The layer to operate on. If not given will default to this.currentLayer.
435+
* @method Phaser.Tileset#setCollisionByExclusion
436+
* @param {array} indexes - An array of the tile IDs to not be counted for collision.
437+
* @param {boolean} [collides=true] - If true it will enable collision. If false it will clear collision.
438+
* @param {number|string|Phaser.TilemapLayer} [layer] - The layer to operate on. If not given will default to this.currentLayer.
425439
*/
426-
setCollisionBetween: function (start, stop, layer) {
440+
setCollisionByExclusion: function (indexes, collides, layer) {
427441

428-
if (start > stop)
429-
{
430-
return;
431-
}
442+
if (typeof collides === 'undefined') { collides = true; }
443+
444+
layer = this.getLayer(layer);
432445

433-
for (var i = start; i <= stop; i++)
446+
// Collide everything, except the IDs given in the indexes array
447+
for (var i = 0, len = this.tiles.length; i < len; i++)
434448
{
435-
var index = this.setCollisionByIndex(i, layer, false);
449+
if (indexes.indexOf(i) === -1)
450+
{
451+
this.setCollisionByIndex(i, collides, layer, false);
452+
}
436453
}
437454

438455
// Now re-calculate interesting faces
439-
this.calculateFaces(index);
456+
this.calculateFaces(layer);
440457

441458
},
442459

443460
/**
444461
* Sets collision values on a tile in the set.
462+
* You shouldn't usually call this method directly, instead use setCollision, setCollisionBetween or setCollisionByExclusion.
445463
*
446464
* @method Phaser.Tileset#setCollisionByIndex
465+
* @protected
447466
* @param {number} index - The index of the tile on the layer.
448-
* @param {number|string|Phaser.TilemapLayer} layer - The layer to operate on. If not given will default to this.currentLayer.
467+
* @param {boolean} [collides=true] - If true it will enable collision on the tile. If false it will clear collision values from the tile.
468+
* @param {number|string|Phaser.TilemapLayer} [layer] - The layer to operate on. If not given will default to this.currentLayer.
449469
* @param {boolean} [recalculate=true] - Recalculates the tile faces after the update.
450470
*/
451-
setCollisionByIndex: function (index, layer, recalculate) {
471+
setCollisionByIndex: function (index, collides, layer, recalculate) {
452472

453-
if (typeof layer === 'undefined')
454-
{
455-
layer = this.currentLayer;
456-
}
457-
else if (typeof layer === 'string')
458-
{
459-
layer = this.getLayerIndex(layer);
460-
}
461-
else if (layer instanceof Phaser.TilemapLayer)
462-
{
463-
layer = layer.index;
464-
}
473+
if (typeof collides === 'undefined') { collides = true; }
474+
if (typeof recalculate === 'undefined') { recalculate = true; }
465475

466-
if (typeof recalculate === "undefined") { recalculate = true; }
476+
layer = this.getLayer(layer);
467477

468478
for (var y = 0; y < this.layers[layer].height ; y++)
469479
{
@@ -473,11 +483,11 @@ Phaser.Tilemap.prototype = {
473483

474484
if (tile && tile.index === index)
475485
{
476-
tile.collides = true;
477-
tile.faceTop = true;
478-
tile.faceBottom = true;
479-
tile.faceLeft = true;
480-
tile.faceRight = true;
486+
tile.collides = collides;
487+
tile.faceTop = collides;
488+
tile.faceBottom = collides;
489+
tile.faceLeft = collides;
490+
tile.faceRight = collides;
481491
}
482492
}
483493
}
@@ -492,12 +502,39 @@ Phaser.Tilemap.prototype = {
492502

493503
},
494504

505+
/**
506+
* Gets the TilemapLayer index as used in the setCollision calls.
507+
*
508+
* @method Phaser.Tileset#getLayer
509+
* @protected
510+
* @param {number|string|Phaser.TilemapLayer} layer - The layer to operate on. If not given will default to this.currentLayer.
511+
* @return {number} The TilemapLayer index.
512+
*/
513+
getLayer: function (layer) {
514+
515+
if (typeof layer === 'undefined')
516+
{
517+
layer = this.currentLayer;
518+
}
519+
else if (typeof layer === 'string')
520+
{
521+
layer = this.getLayerIndex(layer);
522+
}
523+
else if (layer instanceof Phaser.TilemapLayer)
524+
{
525+
layer = layer.index;
526+
}
527+
528+
return layer;
529+
530+
},
531+
495532
/**
496533
* Internal function.
497534
*
498535
* @method Phaser.Tileset#calculateFaces
499536
* @protected
500-
* @param {number} layer - The layer to operate on.
537+
* @param {number} layer - The index of the TilemapLayer to operate on.
501538
*/
502539
calculateFaces: function (layer) {
503540

@@ -680,7 +717,7 @@ Phaser.Tilemap.prototype = {
680717
* @param {number} tileHeight - The height of the tile in pixels.
681718
* @param {number} [layer] - The Tilemap Layer to operate on.
682719
*/
683-
putTileWorldXY: function (index, x, y, tileWidth, tileHeight, layer) {
720+
putTileWorldXY: function (tile, x, y, tileWidth, tileHeight, layer) {
684721

685722
if (typeof layer === "undefined") { layer = this.currentLayer; }
686723

@@ -725,10 +762,7 @@ Phaser.Tilemap.prototype = {
725762
x = this.game.math.snapToFloor(x, tileWidth) / tileWidth;
726763
y = this.game.math.snapToFloor(y, tileHeight) / tileHeight;
727764

728-
if (x >= 0 && x < this.layers[layer].width && y >= 0 && y < this.layers[layer].height)
729-
{
730-
return this.layers[layer].data[y][x];
731-
}
765+
return this.getTile(x, y, layer);
732766

733767
},
734768

0 commit comments

Comments
 (0)