Skip to content

Commit beaac18

Browse files
committed
Active animations now monitor if the game pauses, and resume normally when the game un-pauses (fixes phaserjs#179)
1 parent 36df551 commit beaac18

4 files changed

Lines changed: 60 additions & 6 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ Bug Fixes:
150150
* Updated Input.Mouse to use event.button not event.which, so the const reference from input.mouse.button is correct (thanks grimor)
151151
* Text that was fixedToCamera would 'jitter' if the world scrolled. Now works as expected across all fixed objects.
152152
* Fixed a bug where Sound.play wouldn't pick-up the local loop setting if not specified in the parameter.
153+
* Active animations now monitor if the game pauses, and resume normally when the game un-pauses (fixes #179)
153154

154155

155156
TO DO:

examples/wip/anim1.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
11

2-
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create });
2+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
33

44
function preload() {
55

66
game.load.spritesheet('mummy', 'assets/sprites/metalslug_mummy37x45.png', 37, 45, 18);
77

88
}
99

10+
var mummy;
11+
var anim;
12+
1013
function create() {
1114

12-
// game.stage.backgroundColor = '#239923';
1315
game.stage.backgroundColor = 0xff8855;
1416

15-
var mummy = game.add.sprite(300, 200, 'mummy', 5);
17+
mummy = game.add.sprite(300, 200, 'mummy', 5);
18+
19+
mummy.animations.updateIfVisible = false;
1620

17-
mummy.animations.add('walk');
21+
anim = mummy.animations.add('walk');
1822

19-
mummy.animations.play('walk', 20, true);
23+
anim.play(2, false);
24+
// anim.play(2, true);
2025

2126
}
27+
28+
function update() {
29+
30+
}
31+
32+
function render() {
33+
34+
game.debug.renderText(anim.frame + ' / 17', 32, 32);
35+
36+
}

src/animation/Animation.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ Phaser.Animation = function (game, parent, name, frameData, frames, delay, loope
115115
* @property {Phaser.Frame} currentFrame - The currently displayed frame of the Animation.
116116
*/
117117
this.currentFrame = this._frameData.getFrame(this._frames[this._frameIndex]);
118+
119+
// Set-up some event listeners
120+
this.game.onPause.add(this.onPause, this);
121+
this.game.onResume.add(this.onResume, this);
118122

119123
};
120124

@@ -162,6 +166,7 @@ Phaser.Animation.prototype = {
162166
this.currentFrame = this._frameData.getFrame(this._frames[this._frameIndex]);
163167
this._parent.setTexture(PIXI.TextureCache[this.currentFrame.uuid]);
164168

169+
// TODO: Double check if required
165170
if (this._parent.__tilePattern)
166171
{
167172
this._parent.__tilePattern = false;
@@ -220,6 +225,36 @@ Phaser.Animation.prototype = {
220225

221226
},
222227

228+
/**
229+
* Called when the Game enters a paused state.
230+
*
231+
* @method Phaser.Animation#onPause
232+
* @memberof Phaser.Animation
233+
*/
234+
onPause: function () {
235+
236+
if (this.isPlaying)
237+
{
238+
this._frameDiff = this._timeNextFrame - this.game.time.now;
239+
}
240+
241+
},
242+
243+
/**
244+
* Called when the Game resumes from a paused state.
245+
*
246+
* @method Phaser.Animation#onResume
247+
* @memberof Phaser.Animation
248+
*/
249+
onResume: function () {
250+
251+
if (this.isPlaying)
252+
{
253+
this._timeNextFrame = this.game.time.now + this._frameDiff;
254+
}
255+
256+
},
257+
223258
/**
224259
* Updates this animation. Called automatically by the AnimationManager.
225260
*
@@ -318,6 +353,9 @@ Phaser.Animation.prototype = {
318353
this.currentFrame = null;
319354
this.isPlaying = false;
320355

356+
this.game.onPause.remove(this.onPause, this);
357+
this.game.onResume.remove(this.onResume, this);
358+
321359
},
322360

323361
/**

src/animation/AnimationManager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ Phaser.AnimationManager.prototype = {
251251
*/
252252
update: function () {
253253

254-
if (this.updateIfVisible && this.sprite.visible === false)
254+
if (this.updateIfVisible && !this.sprite.visible)
255255
{
256256
return false;
257257
}

0 commit comments

Comments
 (0)