Skip to content

Commit 3e99391

Browse files
committed
Updated all Game Objects so they all have preUpdate, update and postUpdate functions (even if empty). Updated World so when it iterates through them all it no longer checks if those functions are present before calling them. Was wasting a lot of time doing that before.
1 parent f9a4beb commit 3e99391

6 files changed

Lines changed: 74 additions & 55 deletions

File tree

src/core/Group.js

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
/**
88
* Phaser Group constructor.
99
* @class Phaser.Group
10-
* @classdesc A Group is a container for display objects that allows for fast pooling, recycling and collision checks.
10+
* @classdesc A Group is a container for display objects that allows for fast pooling and object recycling. Groups can be nested within other Groups and have their own local transforms.
1111
* @constructor
1212
* @param {Phaser.Game} game - A reference to the currently running game.
13-
* @param {*} parent - The parent Group, DisplayObject or DisplayObjectContainer that this Group will be added to. If undefined or null it will use game.world.
13+
* @param {Phaser.Group|Phaser.Sprite} parent - The parent Group, DisplayObject or DisplayObjectContainer that this Group will be added to. If undefined or null it will use game.world.
1414
* @param {string} [name=group] - A name for this Group. Not used internally but useful for debugging.
1515
* @param {boolean} [addToStage=false] - If set to true this Group will be added directly to the Game.Stage instead of Game.World.
1616
*/
@@ -708,12 +708,9 @@ Phaser.Group.prototype.callAll = function (method, context) {
708708
*/
709709
Phaser.Group.prototype.preUpdate = function () {
710710

711-
for (var i = 0, len = this.children.length; i < len; i++)
711+
for (var i = this.children.length - 1; i >= 0; i--)
712712
{
713-
if (this.children[i]['preUpdate'])
714-
{
715-
this.children[i].preUpdate();
716-
}
713+
this.children[i].preUpdate();
717714
}
718715

719716
}
@@ -725,12 +722,9 @@ Phaser.Group.prototype.preUpdate = function () {
725722
*/
726723
Phaser.Group.prototype.update = function () {
727724

728-
for (var i = 0, len = this.children.length; i < len; i++)
725+
for (var i = this.children.length - 1; i >= 0; i--)
729726
{
730-
if (this.children[i]['update'])
731-
{
732-
this.children[i].update();
733-
}
727+
this.children[i].update();
734728
}
735729

736730
}
@@ -742,12 +736,9 @@ Phaser.Group.prototype.update = function () {
742736
*/
743737
Phaser.Group.prototype.postUpdate = function () {
744738

745-
for (var i = 0, len = this.children.length; i < len; i++)
739+
for (var i = this.children.length - 1; i >= 0; i--)
746740
{
747-
if (this.children[i]['postUpdate'])
748-
{
749-
this.children[i].postUpdate();
750-
}
741+
this.children[i].postUpdate();
751742
}
752743

753744
}

src/core/World.js

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Phaser.World = function (game) {
3939
*/
4040
this.currentRenderOrderID = 0;
4141

42-
};
42+
}
4343

4444
Phaser.World.prototype = Object.create(Phaser.Group.prototype);
4545
Phaser.World.prototype.constructor = Phaser.World;
@@ -72,12 +72,9 @@ Phaser.World.prototype.preUpdate = function () {
7272

7373
this.currentRenderOrderID = 0;
7474

75-
for (var i = 0, len = this.children.length; i < len; i++)
75+
for (var i = this.children.length - 1; i >= 0; i--)
7676
{
77-
if (this.children[i]['preUpdate'])
78-
{
79-
this.children[i].preUpdate();
80-
}
77+
this.children[i].preUpdate();
8178
}
8279

8380
}
@@ -90,12 +87,9 @@ Phaser.World.prototype.preUpdate = function () {
9087
*/
9188
Phaser.World.prototype.update = function () {
9289

93-
for (var i = 0, len = this.children.length; i < len; i++)
90+
for (var i = this.children.length - 1; i >= 0; i--)
9491
{
95-
if (this.children[i]['update'])
96-
{
97-
this.children[i].update();
98-
}
92+
this.children[i].update();
9993
}
10094

10195
}
@@ -116,24 +110,18 @@ Phaser.World.prototype.postUpdate = function () {
116110

117111
this.camera.update();
118112

119-
for (var i = 0, len = this.children.length; i < len; i++)
113+
for (var i = this.children.length - 1; i >= 0; i--)
120114
{
121-
if (this.children[i]['postUpdate'])
122-
{
123-
this.children[i].postUpdate();
124-
}
115+
this.children[i].postUpdate();
125116
}
126117
}
127118
else
128119
{
129120
this.camera.update();
130121

131-
for (var i = 0, len = this.children.length; i < len; i++)
122+
for (var i = this.children.length - 1; i >= 0; i--)
132123
{
133-
if (this.children[i]['postUpdate'])
134-
{
135-
this.children[i].postUpdate();
136-
}
124+
this.children[i].postUpdate();
137125
}
138126
}
139127

src/gameobjects/Image.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,17 @@ Phaser.Image.prototype.preUpdate = function() {
147147

148148
return true;
149149

150-
};
150+
}
151+
152+
/**
153+
* Override and use this function in your own custom objects to handle any update requirements you may have.
154+
*
155+
* @method Phaser.Image#update
156+
* @memberof Phaser.Image
157+
*/
158+
Phaser.Image.prototype.update = function() {
159+
160+
}
151161

152162
/**
153163
* Internal function called by the World postUpdate cycle.
@@ -168,7 +178,7 @@ Phaser.Image.prototype.postUpdate = function() {
168178
this.position.y = this.game.camera.view.y + this.y;
169179
}
170180

171-
};
181+
}
172182

173183
/**
174184
* Changes the Texture the Sprite is using entirely. The old texture is removed and the new one is referenced or fetched from the Cache.
@@ -245,7 +255,7 @@ Phaser.Image.prototype.loadTexture = function (key, frame) {
245255
}
246256
}
247257

248-
};
258+
}
249259

250260
/**
251261
* Crop allows you to crop the texture used to display this Image.
@@ -292,7 +302,7 @@ Phaser.Image.prototype.crop = function(rect) {
292302
}
293303
}
294304

295-
};
305+
}
296306

297307
/**
298308
* Brings a 'dead' Sprite back to life, optionally giving it the health value specified.
@@ -316,7 +326,7 @@ Phaser.Image.prototype.revive = function() {
316326

317327
return this;
318328

319-
};
329+
}
320330

321331
/**
322332
* Kills a Sprite. A killed Sprite has its alive, exists and visible properties all set to false.
@@ -341,7 +351,7 @@ Phaser.Image.prototype.kill = function() {
341351

342352
return this;
343353

344-
};
354+
}
345355

346356
/**
347357
* Destroys the Sprite. This removes it from its parent group, destroys the input, event and animation handlers if present
@@ -378,7 +388,7 @@ Phaser.Image.prototype.destroy = function() {
378388

379389
this.game = null;
380390

381-
};
391+
}
382392

383393
/**
384394
* Resets the Sprite. This places the Sprite at the given x/y world coordinates and then sets alive, exists, visible and renderable all to true.
@@ -401,7 +411,7 @@ Phaser.Image.prototype.reset = function(x, y) {
401411

402412
return this;
403413

404-
};
414+
}
405415

406416
/**
407417
* Brings the Sprite to the top of the display list it is a child of. Sprites that are members of a Phaser.Group are only
@@ -427,7 +437,7 @@ Phaser.Image.prototype.bringToTop = function(child) {
427437

428438
return this;
429439

430-
};
440+
}
431441

432442
/**
433443
* Indicates the rotation of the Sprite, in degrees, from its original orientation. Values from 0 to 180 represent clockwise rotation; values from 0 to -180 represent counterclockwise rotation.

src/gameobjects/Sprite.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,10 @@ Phaser.Sprite = function (game, x, y, key, frame) {
153153
* 4 = fresh? (0 = no, 1 = yes)
154154
* 5 = outOfBoundsFired (0 = no, 1 = yes)
155155
* 6 = exists (0 = no, 1 = yes)
156-
* @property {array} _cache
156+
* @property {Int16Array} _cache
157157
* @private
158158
*/
159-
this._cache = [0, 0, 0, 0, 1, 0, 1];
159+
this._cache = new Int16Array([0, 0, 0, 0, 1, 0, 1]);
160160

161161
/**
162162
* @property {Phaser.Rectangle} _bounds - Internal cache var.
@@ -272,6 +272,16 @@ Phaser.Sprite.prototype.preUpdate = function() {
272272

273273
};
274274

275+
/**
276+
* Override and use this function in your own custom objects to handle any update requirements you may have.
277+
*
278+
* @method Phaser.Sprite#update
279+
* @memberof Phaser.Sprite
280+
*/
281+
Phaser.Sprite.prototype.update = function() {
282+
283+
};
284+
275285
/**
276286
* Internal function called by the World postUpdate cycle.
277287
*

src/gameobjects/Text.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,16 @@ Phaser.Text.prototype.preUpdate = function () {
121121

122122
}
123123

124+
/**
125+
* Override and use this function in your own custom objects to handle any update requirements you may have.
126+
*
127+
* @method Phaser.Text#update
128+
* @memberof Phaser.Text
129+
*/
130+
Phaser.Text.prototype.update = function() {
131+
132+
}
133+
124134
/**
125135
* Automatically called by World.postUpdate.
126136
* @method Phaser.Text.prototype.postUpdate
@@ -219,7 +229,7 @@ Phaser.Text.prototype.setStyle = function (style) {
219229
this.style = style;
220230
this.dirty = true;
221231

222-
};
232+
}
223233

224234
/**
225235
* Renders text. This replaces the Pixi.Text.updateText function as we need a few extra bits in here.
@@ -299,7 +309,7 @@ Phaser.Text.prototype.updateText = function () {
299309
}
300310

301311
this.updateTexture();
302-
};
312+
}
303313

304314
/**
305315
* Greedy wrapping algorithm that will wrap words as the line grows longer than its horizontal bounds.
@@ -347,7 +357,7 @@ Phaser.Text.prototype.runWordWrap = function (text) {
347357

348358
return result;
349359

350-
};
360+
}
351361

352362
/**
353363
* Indicates the rotation of the Text, in degrees, from its original orientation. Values from 0 to 180 represent clockwise rotation; values from 0 to -180 represent counterclockwise rotation.

src/gameobjects/TileSprite.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@ Phaser.TileSprite.prototype.preUpdate = function() {
119119

120120
}
121121

122+
/**
123+
* Override and use this function in your own custom objects to handle any update requirements you may have.
124+
*
125+
* @method Phaser.TileSprite#update
126+
* @memberof Phaser.TileSprite
127+
*/
128+
Phaser.TileSprite.prototype.update = function() {
129+
130+
}
131+
122132
/**
123133
* Internal function called by the World postUpdate cycle.
124134
*
@@ -232,7 +242,7 @@ Phaser.TileSprite.prototype.loadTexture = function (key, frame) {
232242
}
233243
}
234244

235-
};
245+
}
236246

237247
/**
238248
* Destroys the TileSprite. This removes it from its parent group, destroys the event and animation handlers if present
@@ -262,7 +272,7 @@ Phaser.TileSprite.prototype.destroy = function() {
262272

263273
this.game = null;
264274

265-
};
275+
}
266276

267277
/**
268278
* Play an animation based on the given key. The animation should previously have been added via sprite.animations.add()
@@ -280,7 +290,7 @@ Phaser.TileSprite.prototype.play = function (name, frameRate, loop, killOnComple
280290

281291
return this.animations.play(name, frameRate, loop, killOnComplete);
282292

283-
};
293+
}
284294

285295
/**
286296
* Indicates the rotation of the Sprite, in degrees, from its original orientation. Values from 0 to 180 represent clockwise rotation; values from 0 to -180 represent counterclockwise rotation.

0 commit comments

Comments
 (0)