Skip to content

Commit 9c35dfd

Browse files
committed
ArcadePhysics.Body has a new boolean property enable. If false the body won't be checked for any collision or overlaps, or have its pre or post update methods called. Use this for easy toggling of physics bodies without having to destroy or re-create the Body object itself.
1 parent 5b9bd96 commit 9c35dfd

5 files changed

Lines changed: 15 additions & 8 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ Version 2.0.6 - "Jornhill" - -in development-
7070
* Phaser.Keyboard.lastChar will return the string value of the last key pressed.
7171
* Phaser.Keyboard.lastKey will return the most recently pressed Key object.
7272
* RetroFont.updateOffset allows you to modify the offsetX/Y values used by the font during rendering.
73+
* ArcadePhysics.Body has a new boolean property `enable`. If `false` the body won't be checked for any collision or overlaps, or have its pre or post update methods called. Use this for easy toggling of physics bodies without having to destroy or re-create the Body object itself.
7374

7475

7576
### Bug Fixes

src/gameobjects/Sprite.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ Phaser.Sprite.prototype.preUpdate = function() {
206206
this._cache[1] = this.world.y;
207207
this._cache[2] = this.rotation;
208208

209-
if (this.body)
209+
if (this.body && this.body.enable)
210210
{
211211
this.body.preUpdate();
212212
}
@@ -281,7 +281,7 @@ Phaser.Sprite.prototype.preUpdate = function() {
281281

282282
this.animations.update();
283283

284-
if (this.body)
284+
if (this.body && this.body.enable)
285285
{
286286
this.body.preUpdate();
287287
}
@@ -320,7 +320,7 @@ Phaser.Sprite.prototype.postUpdate = function() {
320320
this.key.render();
321321
}
322322

323-
if (this.exists && this.body)
323+
if (this.exists && this.body && this.body.enable)
324324
{
325325
this.body.postUpdate();
326326
}

src/gameobjects/TileSprite.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ Phaser.TileSprite.prototype.preUpdate = function() {
173173
this._cache[1] = this.world.y;
174174
this._cache[2] = this.rotation;
175175

176-
if (this.body)
176+
if (this.body && this.body.enable)
177177
{
178178
this.body.preUpdate();
179179
}
@@ -241,7 +241,7 @@ Phaser.TileSprite.prototype.preUpdate = function() {
241241
this.tilePosition.y += this._scroll.y * this.game.time.physicsElapsed;
242242
}
243243

244-
if (this.body)
244+
if (this.body && this.body.enable)
245245
{
246246
this.body.preUpdate();
247247
}
@@ -274,7 +274,7 @@ Phaser.TileSprite.prototype.update = function() {
274274
*/
275275
Phaser.TileSprite.prototype.postUpdate = function() {
276276

277-
if (this.exists && this.body)
277+
if (this.exists && this.body && this.body.enable)
278278
{
279279
this.body.postUpdate();
280280
}

src/physics/arcade/Body.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ Phaser.Physics.Arcade.Body = function (sprite) {
3030
*/
3131
this.type = Phaser.Physics.ARCADE;
3232

33+
/**
34+
* @property {boolean} enable - A disabled body won't be checked for any form of collision or overlap or have its pre/post updates run.
35+
* @default
36+
*/
37+
this.enable = true;
38+
3339
/**
3440
* @property {Phaser.Point} offset - The offset of the Physics Body from the Sprite x/y position.
3541
*/

src/physics/arcade/World.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ Phaser.Physics.Arcade.prototype = {
741741
*/
742742
separate: function (body1, body2, processCallback, callbackContext, overlapOnly) {
743743

744-
if (!this.intersects(body1, body2))
744+
if (!body1.enable || !body2.enable || !this.intersects(body1, body2))
745745
{
746746
return false;
747747
}
@@ -1062,7 +1062,7 @@ Phaser.Physics.Arcade.prototype = {
10621062
separateTile: function (i, body, tile) {
10631063

10641064
// We re-check for collision in case body was separated in a previous step
1065-
if (!tile.intersects(body.position.x, body.position.y, body.right, body.bottom))
1065+
if (!body.enable || !tile.intersects(body.position.x, body.position.y, body.right, body.bottom))
10661066
{
10671067
// no collision so bail out (separted in a previous step)
10681068
return false;

0 commit comments

Comments
 (0)