Skip to content

Commit 51d29b8

Browse files
committed
Animation.updateFrame will now call setSizeToFrame on the Game Object, which will adjust the Game Objects width and height properties to match the frame size. Fix phaserjs#3473
1 parent 96507be commit 51d29b8

2 files changed

Lines changed: 40 additions & 15 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ being passed to the simulation. The default value is 1 to remain consistent with
4949
* The ComputedSize Component now has `setSize` and `setDisplaySize` methods. This component is used for Game Objects that have a non-texture based size.
5050
* The GamepadManager now extends EventEmitter directly, just like the KeyboardManager does.
5151
* The Gamepad Axis threshold has been increased from 0.05 to 0.1.
52+
* Animation.updateFrame will now call `setSizeToFrame` on the Game Object, which will adjust the Game Objects `width` and `height` properties to match the frame size. Fix #3473 (thanks @wtravO @jp-gc)
53+
* Animation.updateFrame now supports animation frames with custom pivot points and injects these into the Game Object origin.
5254

5355
Also, my thanks to the following for helping with the Phaser 3 Examples and Docs, either by reporting errors, fixing them or helping author the docs: @gabegordon @melissaelopez @samid737 @nbs @tgrajewski @pagesrichie @hexus @mbrickn @erd0s @icbat @Matthew-Herman
5456

src/gameobjects/components/Animation.js

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -513,13 +513,7 @@ var Animation = new Class({
513513
{
514514
this.stop();
515515

516-
var sprite = this.parent;
517-
var frame = this.currentAnim.frames[0];
518-
519-
this.currentFrame = frame;
520-
521-
sprite.texture = frame.frame.texture;
522-
sprite.frame = frame.frame;
516+
this.setCurrentFrame(this.currentAnim.frames[0]);
523517
}
524518
},
525519

@@ -733,6 +727,40 @@ var Animation = new Class({
733727
}
734728
},
735729

730+
/**
731+
* Sets the given Animation Frame as being the current frame
732+
* and applies it to the parent Game Object, adjusting its size and origin as needed.
733+
*
734+
* @method Phaser.GameObjects.Components.Animation#setCurrentFrame
735+
* @since 3.4.0
736+
*
737+
* @param {Phaser.Animations.AnimationFrame} animationFrame - The Animation Frame to set as being current.
738+
*
739+
* @return {Phaser.GameObjects.GameObject} The Game Object this Animation Component belongs to.
740+
*/
741+
setCurrentFrame: function (animationFrame)
742+
{
743+
var gameObject = this.parent;
744+
745+
this.currentFrame = animationFrame;
746+
747+
gameObject.texture = animationFrame.frame.texture;
748+
gameObject.frame = animationFrame.frame;
749+
750+
gameObject.setSizeToFrame();
751+
752+
if (animationFrame.frame.customPivot)
753+
{
754+
gameObject.setOrigin(animationFrame.frame.pivotX, animationFrame.frame.pivotY);
755+
}
756+
else
757+
{
758+
gameObject.updateDisplayOrigin();
759+
}
760+
761+
return gameObject;
762+
},
763+
736764
/**
737765
* [description]
738766
*
@@ -743,18 +771,13 @@ var Animation = new Class({
743771
*/
744772
updateFrame: function (animationFrame)
745773
{
746-
var sprite = this.parent;
747-
748-
this.currentFrame = animationFrame;
749-
750-
sprite.texture = animationFrame.frame.texture;
751-
sprite.frame = animationFrame.frame;
774+
var gameObject = this.setCurrentFrame(animationFrame);
752775

753776
if (this.isPlaying)
754777
{
755778
if (animationFrame.setAlpha)
756779
{
757-
sprite.alpha = animationFrame.alpha;
780+
gameObject.alpha = animationFrame.alpha;
758781
}
759782

760783
var anim = this.currentAnim;
@@ -766,7 +789,7 @@ var Animation = new Class({
766789

767790
if (animationFrame.onUpdate)
768791
{
769-
animationFrame.onUpdate(sprite, animationFrame);
792+
animationFrame.onUpdate(gameObject, animationFrame);
770793
}
771794
}
772795
},

0 commit comments

Comments
 (0)