Skip to content

Commit 312ec46

Browse files
committed
Sprite.exists now toggles the Body as well. Sprite.exists = false will remove an active Body from the World.
1 parent b85f40d commit 312ec46

3 files changed

Lines changed: 70 additions & 8 deletions

File tree

src/gameobjects/Sprite.js

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@ Phaser.Sprite = function (game, x, y, key, frame) {
3131
* @property {Phaser.Game} game - A reference to the currently running Game.
3232
*/
3333
this.game = game;
34-
35-
/**
36-
* @property {boolean} exists - If exists = false then the Sprite isn't updated by the core game loop or physics subsystem at all.
37-
* @default
38-
*/
39-
this.exists = true;
4034

4135
/**
4236
* @property {string} name - The user defined name given to this Sprite.
@@ -158,10 +152,11 @@ Phaser.Sprite = function (game, x, y, key, frame) {
158152
* 3 = renderID
159153
* 4 = fresh? (0 = no, 1 = yes)
160154
* 5 = outOfBoundsFired (0 = no, 1 = yes)
155+
* 6 = exists (0 = no, 1 = yes)
161156
* @property {array} _cache
162157
* @private
163158
*/
164-
this._cache = [0, 0, 0, 0, 1, 0];
159+
this._cache = [0, 0, 0, 0, 1, 0, 1];
165160

166161
/**
167162
* @property {Phaser.Rectangle} _bounds - Internal cache var.
@@ -864,3 +859,42 @@ Object.defineProperty(Phaser.Sprite.prototype, "physicsEnabled", {
864859
}
865860

866861
});
862+
863+
/**
864+
* @name Phaser.Sprite#exists
865+
* @property {boolean} exists - Sprite.exists controls if the core game loop and physics update this Sprite or not. Set to false to remove from the update and physics world.
866+
*/
867+
Object.defineProperty(Phaser.Sprite.prototype, "exists", {
868+
869+
get: function () {
870+
871+
// hmm, type co-ercing? safe?
872+
return this._cache[6];
873+
874+
},
875+
876+
set: function (value) {
877+
878+
if (value)
879+
{
880+
// exists = true
881+
this._cache[6] = 1;
882+
883+
if (this.body)
884+
{
885+
this.body.addToWorld();
886+
}
887+
}
888+
else
889+
{
890+
// exists = false
891+
this._cache[6] = 0;
892+
893+
if (this.body)
894+
{
895+
this.body.removeFromWorld();
896+
}
897+
}
898+
}
899+
900+
});

src/physics/Body.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,34 @@ Phaser.Physics.Body.prototype = {
309309

310310
},
311311

312+
/**
313+
* Adds this physics body to the world.
314+
*
315+
* @method Phaser.Physics.Body#addToWorld
316+
*/
317+
addToWorld: function () {
318+
319+
if (this.data.world !== this.game.physics)
320+
{
321+
this.game.physics.addBody(this.data);
322+
}
323+
324+
},
325+
326+
/**
327+
* Removes this physics body from the world.
328+
*
329+
* @method Phaser.Physics.Body#removeFromWorld
330+
*/
331+
removeFromWorld: function () {
332+
333+
if (this.data.world === this.game.physics)
334+
{
335+
this.game.physics.removeBody(this.data);
336+
}
337+
338+
},
339+
312340
/**
313341
* Destroys this Body and all references it holds to other objects.
314342
*

tutorials/01 Getting Started/part4.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ <h3>Sublime Text</h3>
2929

3030
<h3>WebStorm</h3>
3131

32-
<p>JetBrains WebStorm is an extremely advanced fully development environment. It goes well beyond simple code editing and offers all of the high-level features you'd expect from a proper IDE include code-insight, npm built-in, code style/syntax reports, source control and a wealth of other features designed more for web developers than game developers. It's based on Eclipse, which is both a good and bad thing. For a start the actual code editing experience is nothing like as smooth and freeform as with Sublime, but the power features can often make up for that. Having errors with your code spotted for you, before you've even tested your game can be really useful. And code-completion is great too, although obviously somewhat limited by the myriad ways JavaScript can be written.</p>
32+
<p>JetBrains WebStorm is an extremely advanced fully development environment. It goes well beyond simple code editing and offers all of the high-level features you'd expect from a proper IDE include code-insight, npm built-in, code style/syntax reports, source control and a wealth of other features designed more for web developers than game developers. It's based on IntelliJ IDEA, which is both a good and bad thing. For a start the actual code editing experience is nothing like as smooth and freeform as with Sublime, but the power features can often make up for that. Having errors with your code spotted for you, before you've even tested your game can be really useful. And code-completion is great too, although obviously somewhat limited by the myriad ways JavaScript can be written.</p>
3333

3434
<p>The full version starts from $49 and is available for Windows and OS X. There are often deals to be found on the JetBrains site too.</p>
3535

0 commit comments

Comments
 (0)