Skip to content

Commit 55e56b1

Browse files
committed
Clock.addEvent can now take an existing TimerEvent object, as well as a config object. If a TimerEvent is given it will be removed from the Clock, reset and then added. This allows you to pool TimerEvents rather than constantly create and delete them. Fix phaserjs#4115
`Clock.removeEvent` is a new method that allows you to remove a `TimerEvent`, or an array of them, from all internal lists of the current Clock.
1 parent 912bed5 commit 55e56b1

1 file changed

Lines changed: 62 additions & 12 deletions

File tree

src/time/Clock.js

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var Class = require('../utils/Class');
88
var PluginCache = require('../plugins/PluginCache');
99
var SceneEvents = require('../scene/events');
1010
var TimerEvent = require('./TimerEvent');
11+
var Remove = require('../utils/array/Remove');
1112

1213
/**
1314
* @classdesc
@@ -55,12 +56,9 @@ var Clock = new Class({
5556
*/
5657
this.now = 0;
5758

58-
// Scale the delta time coming into the Clock by this factor
59-
// which then influences anything using this Clock for calculations, like TimerEvents
60-
6159
/**
6260
* The scale of the Clock's time delta.
63-
*
61+
*
6462
* The time delta is the time elapsed between two consecutive frames and influences the speed of time for this Clock and anything which uses it, such as its Timer Events. Values higher than 1 increase the speed of time, while values smaller than 1 decrease it. A value of 0 freezes time and is effectively equivalent to pausing the Clock.
6563
*
6664
* @name Phaser.Time.Clock#timeScale
@@ -94,7 +92,7 @@ var Clock = new Class({
9492
this._active = [];
9593

9694
/**
97-
* An array of all Timer Events which will be added to the Clock at the start of the frame.
95+
* An array of all Timer Events which will be added to the Clock at the start of the next frame.
9896
*
9997
* @name Phaser.Time.Clock#_pendingInsertion
10098
* @type {Phaser.Time.TimerEvent[]}
@@ -105,7 +103,7 @@ var Clock = new Class({
105103
this._pendingInsertion = [];
106104

107105
/**
108-
* An array of all Timer Events which will be removed from the Clock at the start of the frame.
106+
* An array of all Timer Events which will be removed from the Clock at the start of the next frame.
109107
*
110108
* @name Phaser.Time.Clock#_pendingRemoval
111109
* @type {Phaser.Time.TimerEvent[]}
@@ -131,7 +129,7 @@ var Clock = new Class({
131129
{
132130
// Sync with the TimeStep
133131
this.now = this.systems.game.loop.time;
134-
132+
135133
this.systems.events.once(SceneEvents.DESTROY, this.destroy, this);
136134
},
137135

@@ -156,16 +154,36 @@ var Clock = new Class({
156154
/**
157155
* Creates a Timer Event and adds it to the Clock at the start of the frame.
158156
*
157+
* You can also pass in an existing Timer Event, which will be reset and added to this Clock.
158+
*
159+
* Note that if the Timer Event is being used by _another_ Clock (in another Scene) it will still
160+
* be updated by that Clock as well, so be careful when using this feature.
161+
*
159162
* @method Phaser.Time.Clock#addEvent
160163
* @since 3.0.0
161164
*
162-
* @param {Phaser.Types.Time.TimerEventConfig} config - The configuration for the Timer Event.
165+
* @param {(Phaser.Time.TimerEvent | Phaser.Types.Time.TimerEventConfig)} config - The configuration for the Timer Event, or an existing Timer Event object.
163166
*
164-
* @return {Phaser.Time.TimerEvent} The Timer Event which was created.
167+
* @return {Phaser.Time.TimerEvent} The Timer Event which was created, or passed in.
165168
*/
166169
addEvent: function (config)
167170
{
168-
var event = new TimerEvent(config);
171+
var event;
172+
173+
if (config instanceof Phaser.Time.TimerEvent)
174+
{
175+
event = config;
176+
177+
this.removeEvent(event);
178+
179+
event.elapsed = event.startAt;
180+
event.hasDispatched = false;
181+
event.repeatCount = (event.repeat === -1 || event.loop) ? 999999999999 : event.repeat;
182+
}
183+
else
184+
{
185+
event = new TimerEvent(config);
186+
}
169187

170188
this._pendingInsertion.push(event);
171189

@@ -198,7 +216,7 @@ var Clock = new Class({
198216
* @method Phaser.Time.Clock#clearPendingEvents
199217
* @since 3.0.0
200218
*
201-
* @return {Phaser.Time.Clock} This Clock object.
219+
* @return {this} - This Clock instance.
202220
*/
203221
clearPendingEvents: function ()
204222
{
@@ -207,13 +225,45 @@ var Clock = new Class({
207225
return this;
208226
},
209227

228+
/**
229+
* Removes the given Timer Event, or an array of Timer Events, from this Clock.
230+
*
231+
* The events are removed from all internal lists (active, pending and removal),
232+
* freeing the event up to be re-used.
233+
*
234+
* @method Phaser.Time.Clock#removeEvent
235+
* @since 3.50.0
236+
*
237+
* @param {(Phaser.Time.TimerEvent | Phaser.Time.TimerEvent[])} events - The Timer Event, or an array of Timer Events, to remove from this Clock.
238+
*
239+
* @return {this} - This Clock instance.
240+
*/
241+
removeEvent: function (events)
242+
{
243+
if (!Array.isArray(events))
244+
{
245+
events = [ events ];
246+
}
247+
248+
for (var i = 0; i < events.length; i++)
249+
{
250+
var event = events[i];
251+
252+
Remove(this._pendingRemoval, event);
253+
Remove(this._pendingInsertion, event);
254+
Remove(this._active, event);
255+
}
256+
257+
return this;
258+
},
259+
210260
/**
211261
* Schedules all active Timer Events for removal at the start of the frame.
212262
*
213263
* @method Phaser.Time.Clock#removeAllEvents
214264
* @since 3.0.0
215265
*
216-
* @return {Phaser.Time.Clock} This Clock object.
266+
* @return {this} - This Clock instance.
217267
*/
218268
removeAllEvents: function ()
219269
{

0 commit comments

Comments
 (0)