@@ -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