@@ -66,6 +66,7 @@ Phaser.Tileset = function (name, firstgid, width, height, margin, spacing, prope
6666 * The spacing between each tile in the sheet (in pixels).
6767 * Use `setSpacing` to change.
6868 * @property {integer } tileSpacing
69+ * @readonly
6970 */
7071 this . tileSpacing = spacing | 0 ;
7172
@@ -76,31 +77,31 @@ Phaser.Tileset = function (name, firstgid, width, height, margin, spacing, prope
7677 this . properties = properties || { } ;
7778
7879 /**
79- * The cached image that contains the individual tiles. Use ` setImage` to set.
80+ * The cached image that contains the individual tiles. Use { @link Phaser.Tileset. setImage setImage} to set.
8081 * @property {?object } image
8182 * @readonly
8283 */
8384 // Modified internally
8485 this . image = null ;
8586
8687 /**
87- * The number of rows in the tile sheet .
88+ * The number of tile rows in the the tileset .
8889 * @property {integer }
8990 * @readonly
9091 */
9192 // Modified internally
9293 this . rows = 0 ;
9394
9495 /**
95- * The number of columns in the sheet .
96+ * The number of tile columns in the tileset .
9697 * @property {integer } columns
9798 * @readonly
9899 */
99100 // Modified internally
100101 this . columns = 0 ;
101102
102103 /**
103- * The total number of tiles in the sheet .
104+ * The total number of tiles in the tileset .
104105 * @property {integer } total
105106 * @readonly
106107 */
@@ -178,7 +179,7 @@ Phaser.Tileset.prototype = {
178179 setImage : function ( image ) {
179180
180181 this . image = image ;
181- this . updateTileData ( ) ;
182+ this . updateTileData ( image . width , image . height ) ;
182183
183184 } ,
184185
@@ -195,7 +196,10 @@ Phaser.Tileset.prototype = {
195196 this . tileMargin = margin | 0 ;
196197 this . tileSpacing = spacing | 0 ;
197198
198- this . updateTileData ( ) ;
199+ if ( this . image )
200+ {
201+ this . updateTileData ( this . image . width , this . image . height ) ;
202+ }
199203
200204 } ,
201205
@@ -204,13 +208,33 @@ Phaser.Tileset.prototype = {
204208 *
205209 * @method Phaser.Tileset#updateTileData
206210 * @private
211+ * @param {integer } imageWidth - The (expected) width of the image to slice.
212+ * @param {integer } imageHeight - The (expected) height of the image to slice.
207213 */
208- updateTileData : function ( ) {
214+ updateTileData : function ( imageWidth , imageHeight ) {
215+
216+ // May be fractional values
217+ var rowCount = ( imageHeight - this . tileMargin ) / ( this . tileHeight + this . tileSpacing ) ;
218+ var colCount = ( imageWidth - this . tileMargin ) / ( this . tileWidth + this . tileSpacing ) ;
219+
220+ if ( rowCount % 1 !== 0 || colCount % 1 !== 0 )
221+ {
222+ console . warn ( "Phaser.Tileset - image tile area is not an even multiple of tile size" ) ;
223+ }
224+
225+ // In Tiled a tileset image that is not an even multiple of the tile dimensions
226+ // is truncated - hence the floor when calculating the rows/columns.
227+ rowCount = Math . floor ( rowCount ) ;
228+ colCount = Math . floor ( colCount ) ;
229+
230+ if ( ( this . rows && this . rows !== rowCount ) || ( this . columns && this . columns !== colCount ) )
231+ {
232+ console . warn ( "Phaser.Tileset - actual and expected number of tile rows and columns differ" ) ;
233+ }
209234
210- var image = this . image ;
211- this . rows = Math . round ( ( image . height - this . tileMargin ) / ( this . tileHeight + this . tileSpacing ) ) ;
212- this . columns = Math . round ( ( image . width - this . tileMargin ) / ( this . tileWidth + this . tileSpacing ) ) ;
213- this . total = this . rows * this . columns ;
235+ this . rows = rowCount ;
236+ this . columns = colCount ;
237+ this . total = rowCount * colCount ;
214238
215239 this . drawCoords . length = 0 ;
216240
0 commit comments