Skip to content

Commit 26a55bd

Browse files
committed
SoundManager.destroy is a new method that will destroy all current sounds and reset any callbacks.
StateManager.clearCurrentState now handles the process of clearing down the current state and is now called if the Game is destroyed. Game.destroy now clears the current state, activating its shutdown callback if it had one. It also now destroys the SoundManager, stopping any currently running sounds (phaserjs#1092)
1 parent 69b9e5e commit 26a55bd

4 files changed

Lines changed: 70 additions & 29 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ Version 2.1.0 - "Cairhien" - -in development-
9393
* P2.PointProxy.mx and my values are get and set in meters with no pixel conversion taking place.
9494
* P2.InversePointProxy.mx and my values are get and set in meters with no pixel conversion taking place.
9595
* Pointer.dirty is a new boolean that is set by the InputHandler. It tells the Pointer to re-check all interactive objects it may be over on the next update, regardless if it has moved position or not. This helps solve issues where you may have a Button that on click generates a pop-up window that now obscures the Button (thanks @jflowers45 #882)
96+
* SoundManager.destroy is a new method that will destroy all current sounds and reset any callbacks.
97+
* StateManager.clearCurrentState now handles the process of clearing down the current state and is now called if the Game is destroyed.
98+
* Game.destroy now clears the current state, activating its shutdown callback if it had one. It also now destroys the SoundManager, stopping any currently running sounds (#1092)
9699

97100
### Updates
98101

src/core/Game.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -741,18 +741,20 @@ Phaser.Game.prototype = {
741741
},
742742

743743
/**
744-
* Nuke the entire game from orbit
744+
* Nukes the entire game from orbit.
745745
*
746746
* @method Phaser.Game#destroy
747747
*/
748748
destroy: function () {
749749

750750
this.raf.stop();
751751

752+
this.state.destroy();
753+
this.sound.destroy();
754+
752755
this.scale.destroy();
753756
this.stage.destroy();
754757
this.input.destroy();
755-
this.state.destroy();
756758
this.physics.destroy();
757759

758760
this.state = null;

src/core/StateManager.js

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -305,33 +305,7 @@ Phaser.StateManager.prototype = {
305305
if (this._pendingState && this.game.isBooted)
306306
{
307307
// Already got a state running?
308-
if (this.current)
309-
{
310-
if (this.onShutDownCallback)
311-
{
312-
this.onShutDownCallback.call(this.callbackContext, this.game);
313-
}
314-
315-
this.game.tweens.removeAll();
316-
317-
this.game.camera.reset();
318-
319-
this.game.input.reset(true);
320-
321-
this.game.physics.clear();
322-
323-
this.game.time.removeAll();
324-
325-
if (this._clearWorld)
326-
{
327-
this.game.world.shutdown();
328-
329-
if (this._clearCache === true)
330-
{
331-
this.game.cache.destroy();
332-
}
333-
}
334-
}
308+
this.clearCurrentState();
335309

336310
this.setCurrentState(this._pendingState);
337311

@@ -365,6 +339,44 @@ Phaser.StateManager.prototype = {
365339

366340
},
367341

342+
/**
343+
* This method clears the current State, calling its shutdown callback. The process also removes any active tweens,
344+
* resets the camera, resets input, clears physics, removes timers and if set clears the world and cache too.
345+
*
346+
* @method Phaser.StateManager#clearCurrentState
347+
*/
348+
clearCurrentState: function () {
349+
350+
if (this.current)
351+
{
352+
if (this.onShutDownCallback)
353+
{
354+
this.onShutDownCallback.call(this.callbackContext, this.game);
355+
}
356+
357+
this.game.tweens.removeAll();
358+
359+
this.game.camera.reset();
360+
361+
this.game.input.reset(true);
362+
363+
this.game.physics.clear();
364+
365+
this.game.time.removeAll();
366+
367+
if (this._clearWorld)
368+
{
369+
this.game.world.shutdown();
370+
371+
if (this._clearCache === true)
372+
{
373+
this.game.cache.destroy();
374+
}
375+
}
376+
}
377+
378+
},
379+
368380
/**
369381
* Checks if a given phaser state is valid. A State is considered valid if it has at least one of the core functions: preload, create, update or render.
370382
*
@@ -614,6 +626,8 @@ Phaser.StateManager.prototype = {
614626
*/
615627
destroy: function () {
616628

629+
this.clearCurrentState();
630+
617631
this.callbackContext = null;
618632

619633
this.onInitCallback = null;

src/sound/SoundManager.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,28 @@ Phaser.SoundManager.prototype = {
500500
}
501501
}
502502

503+
},
504+
505+
/**
506+
* Stops all the sounds in the game, then destroys them and finally clears up any callbacks.
507+
*
508+
* @method Phaser.SoundManager#destroy
509+
*/
510+
destroy: function () {
511+
512+
this.stopAll();
513+
514+
for (var i = 0; i < this._sounds.length; i++)
515+
{
516+
if (this._sounds[i])
517+
{
518+
this._sounds[i].destroy();
519+
}
520+
}
521+
522+
this._sounds = [];
523+
this.onSoundDecode.dispose();
524+
503525
}
504526

505527
};

0 commit comments

Comments
 (0)