Sprite.exists now toggles the Body as well. Sprite.exists = false will remove an active Body from the World.

This commit is contained in:
photonstorm 2014-02-13 09:55:46 +00:00
parent b85f40df12
commit 312ec462bc
3 changed files with 70 additions and 8 deletions

View file

@ -31,12 +31,6 @@ Phaser.Sprite = function (game, x, y, key, frame) {
* @property {Phaser.Game} game - A reference to the currently running Game.
*/
this.game = game;
/**
* @property {boolean} exists - If exists = false then the Sprite isn't updated by the core game loop or physics subsystem at all.
* @default
*/
this.exists = true;
/**
* @property {string} name - The user defined name given to this Sprite.
@ -158,10 +152,11 @@ Phaser.Sprite = function (game, x, y, key, frame) {
* 3 = renderID
* 4 = fresh? (0 = no, 1 = yes)
* 5 = outOfBoundsFired (0 = no, 1 = yes)
* 6 = exists (0 = no, 1 = yes)
* @property {array} _cache
* @private
*/
this._cache = [0, 0, 0, 0, 1, 0];
this._cache = [0, 0, 0, 0, 1, 0, 1];
/**
* @property {Phaser.Rectangle} _bounds - Internal cache var.
@ -864,3 +859,42 @@ Object.defineProperty(Phaser.Sprite.prototype, "physicsEnabled", {
}
});
/**
* @name Phaser.Sprite#exists
* @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.
*/
Object.defineProperty(Phaser.Sprite.prototype, "exists", {
get: function () {
// hmm, type co-ercing? safe?
return this._cache[6];
},
set: function (value) {
if (value)
{
// exists = true
this._cache[6] = 1;
if (this.body)
{
this.body.addToWorld();
}
}
else
{
// exists = false
this._cache[6] = 0;
if (this.body)
{
this.body.removeFromWorld();
}
}
}
});

View file

@ -309,6 +309,34 @@ Phaser.Physics.Body.prototype = {
},
/**
* Adds this physics body to the world.
*
* @method Phaser.Physics.Body#addToWorld
*/
addToWorld: function () {
if (this.data.world !== this.game.physics)
{
this.game.physics.addBody(this.data);
}
},
/**
* Removes this physics body from the world.
*
* @method Phaser.Physics.Body#removeFromWorld
*/
removeFromWorld: function () {
if (this.data.world === this.game.physics)
{
this.game.physics.removeBody(this.data);
}
},
/**
* Destroys this Body and all references it holds to other objects.
*

View file

@ -29,7 +29,7 @@
<h3>WebStorm</h3>
<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>
<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>
<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>