Skip to content

Commit c2f7520

Browse files
committed
StateManager.onStateChange is a new signal which is dispatched whenever the State changes from one to another. The callback you specify is sent two parameters: the string based key of the new state, and the second parameter is the string based key of the old / previous state.
1 parent 5054344 commit c2f7520

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

src/core/StateManager.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ Phaser.StateManager = function (game, pendingState) {
6767
*/
6868
this.current = '';
6969

70+
/**
71+
* onStateChange is a Phaser.Signal that is dispatched whenever the game changes state.
72+
*
73+
* It is dispatched only when the new state is started, which isn't usually at the same time as StateManager.start
74+
* is called because state swapping is done in sync with the game loop. It is dispatched *before* any of the new states
75+
* methods (such as preload and create) are called, and *after* the previous states shutdown method has been run.
76+
*
77+
* The callback you specify is sent two parameters: the string based key of the new state,
78+
* and the second parameter is the string based key of the old / previous state.
79+
*
80+
* @property {Phaser.Signal} onStateChange
81+
*/
82+
this.onStateChange = new Phaser.Signal();
83+
7084
/**
7185
* @property {function} onInitCallback - This is called when the state is set as the active state.
7286
* @default
@@ -316,11 +330,15 @@ Phaser.StateManager.prototype = {
316330

317331
if (this._pendingState && this.game.isBooted)
318332
{
333+
var previousStateKey = this.current;
334+
319335
// Already got a state running?
320336
this.clearCurrentState();
321337

322338
this.setCurrentState(this._pendingState);
323339

340+
this.onStateChange.dispatch(this.current, this.previousStateKey);
341+
324342
if (this.current !== this._pendingState)
325343
{
326344
return;

0 commit comments

Comments
 (0)