Skip to content

Commit 24e8f7a

Browse files
committed
Body.destroy is now flag based, cleared by the World.
Added pendingDestroy flag to handle body destruction within events or collider iterations.
1 parent 81d0a48 commit 24e8f7a

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

src/physics/arcade/Body.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ var Body = new Class({
8484
*/
8585
this.enable = true;
8686

87+
/**
88+
* If Body.destroy is called during the main physics update loop then this flag is set.
89+
* The Body is then actually destroyed during World.postUpdate.
90+
* You can also toggle it yourself.
91+
*
92+
* @name Phaser.Physics.Arcade.Body#pendingDestroy
93+
* @type {boolean}
94+
* @default false
95+
* @since 3.0.0
96+
*/
97+
this.pendingDestroy = false;
98+
8799
/**
88100
* [description]
89101
*
@@ -1267,8 +1279,17 @@ var Body = new Class({
12671279
*/
12681280
destroy: function ()
12691281
{
1270-
this.gameObject.body = null;
1271-
this.gameObject = null;
1282+
if (!this.pendingDestroy)
1283+
{
1284+
// Will be removed the next time World.postUpdate runs, not before.
1285+
this.pendingDestroy = true;
1286+
}
1287+
else
1288+
{
1289+
this.world.disableBody(this);
1290+
1291+
this.world = null;
1292+
}
12721293
},
12731294

12741295
/**

0 commit comments

Comments
 (0)