From 26a55bd2022c7626498c06aea2a844aef103ae39 Mon Sep 17 00:00:00 2001 From: photonstorm Date: Fri, 29 Aug 2014 11:37:47 +0100 Subject: [PATCH] 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 (#1092) --- README.md | 3 ++ src/core/Game.js | 6 ++-- src/core/StateManager.js | 68 +++++++++++++++++++++++---------------- src/sound/SoundManager.js | 22 +++++++++++++ 4 files changed, 70 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 96e38a401..a6a2b6a06 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,9 @@ Version 2.1.0 - "Cairhien" - -in development- * P2.PointProxy.mx and my values are get and set in meters with no pixel conversion taking place. * P2.InversePointProxy.mx and my values are get and set in meters with no pixel conversion taking place. * 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) +* 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 (#1092) ### Updates diff --git a/src/core/Game.js b/src/core/Game.js index a06a06c1e..7fdd64803 100644 --- a/src/core/Game.js +++ b/src/core/Game.js @@ -741,7 +741,7 @@ Phaser.Game.prototype = { }, /** - * Nuke the entire game from orbit + * Nukes the entire game from orbit. * * @method Phaser.Game#destroy */ @@ -749,10 +749,12 @@ Phaser.Game.prototype = { this.raf.stop(); + this.state.destroy(); + this.sound.destroy(); + this.scale.destroy(); this.stage.destroy(); this.input.destroy(); - this.state.destroy(); this.physics.destroy(); this.state = null; diff --git a/src/core/StateManager.js b/src/core/StateManager.js index 75d84af3a..14c029446 100644 --- a/src/core/StateManager.js +++ b/src/core/StateManager.js @@ -305,33 +305,7 @@ Phaser.StateManager.prototype = { if (this._pendingState && this.game.isBooted) { // Already got a state running? - if (this.current) - { - if (this.onShutDownCallback) - { - this.onShutDownCallback.call(this.callbackContext, this.game); - } - - this.game.tweens.removeAll(); - - this.game.camera.reset(); - - this.game.input.reset(true); - - this.game.physics.clear(); - - this.game.time.removeAll(); - - if (this._clearWorld) - { - this.game.world.shutdown(); - - if (this._clearCache === true) - { - this.game.cache.destroy(); - } - } - } + this.clearCurrentState(); this.setCurrentState(this._pendingState); @@ -365,6 +339,44 @@ Phaser.StateManager.prototype = { }, + /** + * This method clears the current State, calling its shutdown callback. The process also removes any active tweens, + * resets the camera, resets input, clears physics, removes timers and if set clears the world and cache too. + * + * @method Phaser.StateManager#clearCurrentState + */ + clearCurrentState: function () { + + if (this.current) + { + if (this.onShutDownCallback) + { + this.onShutDownCallback.call(this.callbackContext, this.game); + } + + this.game.tweens.removeAll(); + + this.game.camera.reset(); + + this.game.input.reset(true); + + this.game.physics.clear(); + + this.game.time.removeAll(); + + if (this._clearWorld) + { + this.game.world.shutdown(); + + if (this._clearCache === true) + { + this.game.cache.destroy(); + } + } + } + + }, + /** * 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. * @@ -614,6 +626,8 @@ Phaser.StateManager.prototype = { */ destroy: function () { + this.clearCurrentState(); + this.callbackContext = null; this.onInitCallback = null; diff --git a/src/sound/SoundManager.js b/src/sound/SoundManager.js index db4b6e470..02538afcd 100644 --- a/src/sound/SoundManager.js +++ b/src/sound/SoundManager.js @@ -500,6 +500,28 @@ Phaser.SoundManager.prototype = { } } + }, + + /** + * Stops all the sounds in the game, then destroys them and finally clears up any callbacks. + * + * @method Phaser.SoundManager#destroy + */ + destroy: function () { + + this.stopAll(); + + for (var i = 0; i < this._sounds.length; i++) + { + if (this._sounds[i]) + { + this._sounds[i].destroy(); + } + } + + this._sounds = []; + this.onSoundDecode.dispose(); + } };