Skip to content

Commit e48d6bf

Browse files
committed
If you pass zero as the width or height when creating a TileSprite it will now use the dimensions of the texture frame as the size of the TileSprite. Fix phaserjs#4073
1 parent 1b85512 commit e48d6bf

4 files changed

Lines changed: 25 additions & 13 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
* `WebGLRenderer.deleteTexture` will check to see if the texture it is being asked to delete is the currently bound texture or not. If it is, it'll set the blank texture to be bound after deletion. This should stop `RENDER WARNING: there is no texture bound to the unit 0` errors if you destroy a Game Object, such as Text or TileSprite, from an async or timed process (thanks jamespierce)
1212
* The `RequestAnimationFrame.step` and `stepTimeout` functions have been updated so that the new Frame is requested from raf before the main game step is called. This allows you to now stop the raf callback from within the game update or render loop. Fix #3952 (thanks @tolimeh)
13+
* If you pass zero as the width or height when creating a TileSprite it will now use the dimensions of the texture frame as the size of the TileSprite. Fix #4073 (thanks @jcyuan)
1314

1415
### Bug Fixes
1516

src/gameobjects/tilesprite/TileSprite.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ var _FLAG = 8; // 1000
6464
* @param {Phaser.Scene} scene - The Scene to which this Game Object belongs. A Game Object can only belong to one Scene at a time.
6565
* @param {number} x - The horizontal position of this Game Object in the world.
6666
* @param {number} y - The vertical position of this Game Object in the world.
67-
* @param {number} width - The width of the Game Object.
68-
* @param {number} height - The height of the Game Object.
67+
* @param {integer} width - The width of the Game Object. If zero it will use the size of the texture frame.
68+
* @param {integer} height - The height of the Game Object. If zero it will use the size of the texture frame.
6969
* @param {string} textureKey - The key of the Texture this Game Object will use to render with, as stored in the Texture Manager.
7070
* @param {(string|integer)} [frameKey] - An optional frame from the Texture this Game Object is rendering with.
7171
*/
@@ -96,13 +96,24 @@ var TileSprite = new Class({
9696

9797
function TileSprite (scene, x, y, width, height, textureKey, frameKey)
9898
{
99-
width = Math.floor(width);
100-
height = Math.floor(height);
101-
10299
var renderer = scene.sys.game.renderer;
103100

104101
GameObject.call(this, scene, 'TileSprite');
105102

103+
var displayTexture = scene.sys.textures.get(textureKey);
104+
var displayFrame = displayTexture.get(frameKey);
105+
106+
if (!width || !height)
107+
{
108+
width = displayFrame.width;
109+
height = displayFrame.height;
110+
}
111+
else
112+
{
113+
width = Math.floor(width);
114+
height = Math.floor(height);
115+
}
116+
106117
/**
107118
* Internal tile position vector.
108119
*
@@ -172,7 +183,7 @@ var TileSprite = new Class({
172183
* @private
173184
* @since 3.12.0
174185
*/
175-
this.displayTexture = scene.sys.textures.get(textureKey);
186+
this.displayTexture = displayTexture;
176187

177188
/**
178189
* The Frame the TileSprite is using as its fill pattern.
@@ -182,7 +193,7 @@ var TileSprite = new Class({
182193
* @private
183194
* @since 3.12.0
184195
*/
185-
this.displayFrame = this.displayTexture.get(frameKey);
196+
this.displayFrame = displayFrame;
186197

187198
/**
188199
* The internal crop data object, as used by `setCrop` and passed to the `Frame.setCropUVs` method.
@@ -219,7 +230,7 @@ var TileSprite = new Class({
219230
* @type {integer}
220231
* @since 3.0.0
221232
*/
222-
this.potWidth = GetPowerOfTwo(this.displayFrame.width);
233+
this.potWidth = GetPowerOfTwo(displayFrame.width);
223234

224235
/**
225236
* The next power of two value from the height of the Fill Pattern frame.
@@ -228,7 +239,7 @@ var TileSprite = new Class({
228239
* @type {integer}
229240
* @since 3.0.0
230241
*/
231-
this.potHeight = GetPowerOfTwo(this.displayFrame.height);
242+
this.potHeight = GetPowerOfTwo(displayFrame.height);
232243

233244
/**
234245
* The Canvas that the TileSprites texture is rendered to.

src/gameobjects/tilesprite/TileSpriteCreator.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ var TileSprite = require('./TileSprite');
1515
*
1616
* @property {number} [x=0] - The x coordinate of the Tile Sprite.
1717
* @property {number} [y=0] - The y coordinate of the Tile Sprite.
18-
* @property {number} [width=512] - The width of the Tile Sprite.
19-
* @property {number} [height=512] - The height of the Tile Sprite.
18+
* @property {integer} [width=512] - The width of the Tile Sprite. If zero it will use the size of the texture frame.
19+
* @property {integer} [height=512] - The height of the Tile Sprite. If zero it will use the size of the texture frame.
2020
* @property {string} [key=''] - The key of the Texture this Tile Sprite will use to render with, as stored in the Texture Manager.
2121
* @property {string} [frame=''] - An optional frame from the Texture this Tile Sprite is rendering with.
2222
*/

src/gameobjects/tilesprite/TileSpriteFactory.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ var GameObjectFactory = require('../GameObjectFactory');
1717
*
1818
* @param {number} x - The horizontal position of this Game Object in the world.
1919
* @param {number} y - The vertical position of this Game Object in the world.
20-
* @param {number} width - The width of the Game Object.
21-
* @param {number} height - The height of the Game Object.
20+
* @param {integer} width - The width of the Game Object. If zero it will use the size of the texture frame.
21+
* @param {integer} height - The height of the Game Object. If zero it will use the size of the texture frame.
2222
* @param {string} texture - The key of the Texture this Game Object will use to render with, as stored in the Texture Manager.
2323
* @param {(string|integer)} [frame] - An optional frame from the Texture this Game Object is rendering with.
2424
*

0 commit comments

Comments
 (0)