Skip to content

Commit 4de9690

Browse files
committed
The setFrame method of the Texture component has been updated so that it will now automatically reset the width and height of a Game Object to match that of the new Frame. Related, it will also adjust the display origin values, because they are size based. If the Frame has a custom pivot it will set the origin to match the custom pivot instead.
1 parent 98930de commit 4de9690

4 files changed

Lines changed: 40 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* If you set `transparent` in the Game Config but didn't provide a `backgroundColor` then it would render as black. It will now be properly transparent. If you do provide a color value then it must include an alpha component.
3838
* You can now pass normal Groups to Arcade Physics collide / overlap, as well as Physics Groups. Fix #3277 (thanks @nkholski)
3939
* Texture.get has been optimized to fail first, then error, with a new falsey check. This allows you to skip out specifying animation frames in the animation config without generating a console warning.
40-
40+
* The `setFrame` method of the Texture component has been updated so that it will now automatically reset the `width` and `height` of a Game Object to match that of the new Frame. Related, it will also adjust the display origin values, because they are size based. If the Frame has a custom pivot it will set the origin to match the custom pivot instead.
4141
* Documentation updates: thanks to @melissaelopez @samme
4242

4343

src/gameobjects/components/Origin.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@
1616

1717
var Origin = {
1818

19+
/**
20+
* A property indicating that a Game Object has this component.
21+
*
22+
* @name Phaser.GameObjects.Components.Origin#_originComponent
23+
* @type {boolean}
24+
* @private
25+
* @default true
26+
* @since 3.2.0
27+
*/
28+
_originComponent: true,
29+
1930
/**
2031
* The horizontal origin of this Game Object.
2132
* The origin maps the relationship between the size and position of the Game Object.

src/gameobjects/components/Size.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@
1313

1414
var Size = {
1515

16+
/**
17+
* A property indicating that a Game Object has this component.
18+
*
19+
* @name Phaser.GameObjects.Components.Size#_sizeComponent
20+
* @type {boolean}
21+
* @private
22+
* @default true
23+
* @since 3.2.0
24+
*/
25+
_sizeComponent: true,
26+
1627
/**
1728
* The native (un-scaled) width of this Game Object.
1829
*

src/gameobjects/components/Texture.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ var Texture = {
6060
* The Frame has to belong to the current Texture being used.
6161
*
6262
* It can be either a string or an index.
63+
*
64+
* Calling `setFrame` will modify the `width` and `height` properties of your Game Object.
65+
* It will also change the `origin` if the Frame has a custom pivot point, as exported from packages like Texture Packer.
6366
*
6467
* @method Phaser.GameObjects.Components.Texture#setFrame
6568
* @since 3.0.0
@@ -81,9 +84,21 @@ var Texture = {
8184
this.renderFlags |= _FLAG;
8285
}
8386

84-
if (this.frame.customPivot)
87+
if (this._sizeComponent)
88+
{
89+
this.setSizeToFrame();
90+
}
91+
92+
if (this._originComponent)
8593
{
86-
this.setOrigin(this.frame.pivotX, this.frame.pivotY);
94+
if (this.frame.customPivot)
95+
{
96+
this.setOrigin(this.frame.pivotX, this.frame.pivotY);
97+
}
98+
else
99+
{
100+
this.updateDisplayOrigin();
101+
}
87102
}
88103

89104
return this;

0 commit comments

Comments
 (0)