From 616d17fa83b2b60bcb13b61858105ca1f0b67a7c Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Fri, 13 Apr 2018 20:12:29 +0100 Subject: [PATCH] Sorted out the scene event order, targets and callbacks --- src/scene/SceneManager.js | 34 +++++++++++++++++++++++++++++----- src/scene/ScenePlugin.js | 36 ++++++++++++++++++++++++++++-------- src/scene/Settings.js | 5 +++++ 3 files changed, 62 insertions(+), 13 deletions(-) diff --git a/src/scene/SceneManager.js b/src/scene/SceneManager.js index 66024f327..ee3970a61 100644 --- a/src/scene/SceneManager.js +++ b/src/scene/SceneManager.js @@ -429,16 +429,28 @@ var SceneManager = new Class({ */ bootScene: function (scene) { + var sys = scene.sys; + var settings = sys.settings; + if (scene.init) { - scene.init.call(scene, scene.sys.settings.data); + scene.init.call(scene, settings.data); + + if (settings.isTransition && sys.events.listenerCount('transitionstart') > 0) + { + // There are listeners waiting for the event after 'init' has run, so emit it + sys.events.emit('transitionstart', settings.transitionFrom); + + settings.isTransition = false; + settings.transitionFrom = null; + } } var loader; - if (scene.sys.load) + if (sys.load) { - loader = scene.sys.load; + loader = sys.load; loader.reset(); } @@ -454,7 +466,7 @@ var SceneManager = new Class({ } else { - scene.sys.settings.status = CONST.LOADING; + settings.status = CONST.LOADING; // Start the loader going as we have something in the queue loader.once('complete', this.loadComplete, this); @@ -583,14 +595,26 @@ var SceneManager = new Class({ */ create: function (scene) { + var sys = scene.sys; + var settings = sys.settings; + if (scene.create) { scene.sys.settings.status = CONST.CREATING; scene.create.call(scene, scene.sys.settings.data); + + if (settings.isTransition && sys.events.listenerCount('transitionstart') > 0) + { + // There are listeners waiting for the event after 'init' has run, so emit it + sys.events.emit('transitionstart', settings.transitionFrom); + + settings.isTransition = false; + settings.transitionFrom = null; + } } - scene.sys.settings.status = CONST.RUNNING; + settings.status = CONST.RUNNING; }, /** diff --git a/src/scene/ScenePlugin.js b/src/scene/ScenePlugin.js index 83e91497d..13824e53a 100644 --- a/src/scene/ScenePlugin.js +++ b/src/scene/ScenePlugin.js @@ -84,11 +84,11 @@ var ScenePlugin = new Class({ * Transition elapsed timer. * * @name Phaser.Scenes.ScenePlugin#_target - * @type {Phaser.Scenes.Scene} + * @type {?Phaser.Scenes.Scene} * @private * @since 3.4.1 */ - this._target; + this._target = null; /** * Transition duration. @@ -216,10 +216,17 @@ var ScenePlugin = new Class({ transition: function (key, duration, moveAbove, callback, context) { if (duration === undefined) { duration = 1000; } + if (moveAbove === undefined) { moveAbove = false; } + if (context === undefined) { context = this.scene; } var target = this.get(key); - if (target && this.settings.status === CONST.RUNNING && this._duration === 0) + if ( + target && + !target.sys.isActive() && + !target.sys.settings.isTransition && + this.settings.status === CONST.RUNNING && + !this._target) { this._elapsed = 0; this._target = target; @@ -227,13 +234,15 @@ var ScenePlugin = new Class({ this._onUpdate = callback; this._onUpdateScope = context; - // Do it via the manager? - // this.manager.transition(from, to, duration, moveAbove, data); + target.sys.settings.isTransition = true; + target.sys.settings.transitionFrom = this.scene; this.manager.start(key); - // Needs storing in manager data, as fires too early here - target.sys.events.emit('transitionstart', this.scene, duration); + if (moveAbove) + { + this.manager.moveAbove(this.key, key); + } this.systems.events.on('postupdate', this.step, this); } @@ -256,6 +265,11 @@ var ScenePlugin = new Class({ { this._elapsed += delta; + if (this._onUpdate) + { + this._onUpdate.call(this._onUpdateScope, time, delta); + } + if (this._elapsed >= this._duration) { // Stop the step @@ -264,6 +278,10 @@ var ScenePlugin = new Class({ // Notify target scene this._target.sys.events.emit('transitioncomplete', this.scene); + // Clear the target out + this._target.sys.settings.isTransition = false; + this._target.sys.settings.transitionFrom = null; + // Stop this Scene this.manager.stop(this.key); } @@ -728,7 +746,9 @@ var ScenePlugin = new Class({ */ shutdown: function () { - this._duration = 0; + this._target = null; + this._onUpdate = null; + this._onUpdateScope = null; var eventEmitter = this.systems.events; diff --git a/src/scene/Settings.js b/src/scene/Settings.js index 885f755d1..6f70494df 100644 --- a/src/scene/Settings.js +++ b/src/scene/Settings.js @@ -34,6 +34,8 @@ var InjectionMap = require('./InjectionMap'); * @property {boolean} active - [description] * @property {boolean} visible - [description] * @property {boolean} isBooted - [description] + * @property {boolean} isTransition - [description] + * @property {?Phaser.Scene} transitionFrom - [description] * @property {object} data - [description] * @property {(false|LoaderFileObject[])} files - [description] * @property {?(InputJSONCameraObject|InputJSONCameraObject[])} cameras - [description] @@ -77,6 +79,9 @@ var Settings = { isBooted: false, + isTransition: false, + transitionFrom: null, + // Loader payload array data: {},