Skip to content

Commit bc93416

Browse files
committed
Added events property and new add and remove callbacks
* `DisplayList.events` is a new property that references the Scene's Event Emitter. This is now used internally. * `DisplayList.addChildCallback` is a new method that overrides the List callback and fires the new ADDED events. * `DisplayList.removeChildCallback` is a new method that overrides the List callback and fires the new REMOVED events.
1 parent ae4ed0a commit bc93416

1 file changed

Lines changed: 62 additions & 9 deletions

File tree

src/gameobjects/DisplayList.js

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
var Class = require('../utils/Class');
88
var List = require('../structs/List');
99
var PluginCache = require('../plugins/PluginCache');
10+
var GameObjectEvents = require('./events');
1011
var SceneEvents = require('../scene/events');
1112
var StableSort = require('../utils/array/StableSort');
1213

@@ -64,8 +65,21 @@ var DisplayList = new Class({
6465
*/
6566
this.systems = scene.sys;
6667

67-
scene.sys.events.once(SceneEvents.BOOT, this.boot, this);
68-
scene.sys.events.on(SceneEvents.START, this.start, this);
68+
/**
69+
* The Scene's Event Emitter.
70+
*
71+
* @name Phaser.GameObjects.DisplayList#events
72+
* @type {Phaser.Events.EventEmitter}
73+
* @since 3.50.0
74+
*/
75+
this.events = scene.sys.events;
76+
77+
// Set the List callbacks
78+
this.addCallback = this.addChildCallback;
79+
this.removeCallback = this.removeChildCallback;
80+
81+
this.events.once(SceneEvents.BOOT, this.boot, this);
82+
this.events.on(SceneEvents.START, this.start, this);
6983
},
7084

7185
/**
@@ -78,7 +92,43 @@ var DisplayList = new Class({
7892
*/
7993
boot: function ()
8094
{
81-
this.systems.events.once(SceneEvents.DESTROY, this.destroy, this);
95+
this.events.once(SceneEvents.DESTROY, this.destroy, this);
96+
},
97+
98+
/**
99+
* Internal method called from `List.addCallback`.
100+
*
101+
* @method Phaser.GameObjects.DisplayList#addChildCallback
102+
* @private
103+
* @fires Phaser.Scenes.Events#ADDED_TO_SCENE
104+
* @fires Phaser.GameObjects.Events#ADDED_TO_SCENE
105+
* @since 3.50.0
106+
*
107+
* @param {Phaser.GameObjects.GameObject} gameObject - The Game Object that was added to the list.
108+
*/
109+
addChildCallback: function (gameObject)
110+
{
111+
gameObject.emit(GameObjectEvents.ADDED_TO_SCENE, gameObject, this.scene);
112+
113+
this.events.emit(SceneEvents.ADDED_TO_SCENE, gameObject, this.scene);
114+
},
115+
116+
/**
117+
* Internal method called from `List.removeCallback`.
118+
*
119+
* @method Phaser.GameObjects.DisplayList#removeChildCallback
120+
* @private
121+
* @fires Phaser.Scenes.Events#REMOVED_FROM_SCENE
122+
* @fires Phaser.GameObjects.Events#REMOVED_FROM_SCENE
123+
* @since 3.50.0
124+
*
125+
* @param {Phaser.GameObjects.GameObject} gameObject - The Game Object that was removed from the list.
126+
*/
127+
removeChildCallback: function (gameObject)
128+
{
129+
gameObject.emit(GameObjectEvents.REMOVED_FROM_SCENE, gameObject, this.scene);
130+
131+
this.events.emit(SceneEvents.REMOVED_FROM_SCENE, gameObject, this.scene);
82132
},
83133

84134
/**
@@ -92,7 +142,7 @@ var DisplayList = new Class({
92142
*/
93143
start: function ()
94144
{
95-
this.systems.events.once(SceneEvents.SHUTDOWN, this.shutdown, this);
145+
this.events.once(SceneEvents.SHUTDOWN, this.shutdown, this);
96146
},
97147

98148
/**
@@ -162,16 +212,18 @@ var DisplayList = new Class({
162212
*/
163213
shutdown: function ()
164214
{
165-
var i = this.list.length;
215+
var list = this.list;
216+
217+
var i = list.length;
166218

167219
while (i--)
168220
{
169-
this.list[i].destroy(true);
221+
list[i].destroy(true);
170222
}
171223

172-
this.list.length = 0;
224+
list.length = 0;
173225

174-
this.systems.events.off(SceneEvents.SHUTDOWN, this.shutdown, this);
226+
this.events.off(SceneEvents.SHUTDOWN, this.shutdown, this);
175227
},
176228

177229
/**
@@ -186,10 +238,11 @@ var DisplayList = new Class({
186238
{
187239
this.shutdown();
188240

189-
this.scene.sys.events.off(SceneEvents.START, this.start, this);
241+
this.events.off(SceneEvents.START, this.start, this);
190242

191243
this.scene = null;
192244
this.systems = null;
245+
this.events = null;
193246
}
194247

195248
});

0 commit comments

Comments
 (0)