Skip to content

Commit 61429d8

Browse files
committed
StateManager.restart allows you to quickly restart the *current* state, optionally clearing the world and cache.
1 parent a4ed94e commit 61429d8

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ New Features
9191
* Sound.destroy will remove a sound and all local references it holds, optionally removing itself from the SoundManager as well.
9292
* SoundManager.removeByKey(key) will remove all sounds from the SoundManager that have a key matching the given value.
9393
* ArcadePhysics.Body.hitTest(x, y) will return a boolean based on if the given world coordinate are within the Body or not.
94+
* StateManager.restart allows you to quickly restart the *current* state, optionally clearing the world and cache.
9495

9596

9697
Bug Fixes

src/core/StateManager.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ Phaser.StateManager.prototype = {
206206
*/
207207
remove: function (key) {
208208

209-
if (this.current == key)
209+
if (this.current === key)
210210
{
211211
this.callbackContext = null;
212212

@@ -257,6 +257,31 @@ Phaser.StateManager.prototype = {
257257

258258
},
259259

260+
/**
261+
* Restarts the current State. State.shutDown will be called (if it exists) before the State is restarted.
262+
*
263+
* @method Phaser.StateManager#restart
264+
* @param {boolean} [clearWorld=true] - Clear everything in the world? This clears the World display list fully (but not the Stage, so if you've added your own objects to the Stage they will need managing directly)
265+
* @param {boolean} [clearCache=false] - Clear the Game.Cache? This purges out all loaded assets. The default is false and you must have clearWorld=true if you want to clearCache as well.
266+
* @param {...*} parameter - Additional parameters that will be passed to the State.init function if it has one.
267+
*/
268+
restart: function (clearWorld, clearCache) {
269+
270+
if (typeof clearWorld === "undefined") { clearWorld = true; }
271+
if (typeof clearCache === "undefined") { clearCache = false; }
272+
273+
// Place the state in the queue. It will be started the next time the game loop starts.
274+
this._pendingState = this.current;
275+
this._clearWorld = clearWorld;
276+
this._clearCache = clearCache;
277+
278+
if (arguments.length > 3)
279+
{
280+
this._args = Array.prototype.splice.call(arguments, 3);
281+
}
282+
283+
},
284+
260285
/**
261286
* Used by onInit and onShutdown when those functions don't exist on the state
262287
* @method Phaser.StateManager#dummy

0 commit comments

Comments
 (0)