Skip to content

Commit a01cc2e

Browse files
committed
Objects with an InputHandler now deactivate it when the object is removed from a Group but not destroyed (fix phaserjs#672)
Lots of jsdoc fixes in Body and World. Removed un-used events from World (such as onImpact).
1 parent 289b5b2 commit a01cc2e

5 files changed

Lines changed: 118 additions & 35 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ New Features
120120
* Emitter.bringToTop and Emitter.sendToBack are booleans that let you optionally set the display order of the Particle when emitted.
121121
* Emitter now calls the Phaser.Particle.onEmit function, which is left empty for you to override and add in custom behaviours.
122122
* p2.World has a new contactMaterial property, which can be configured like a normal P2 Contact Material and is applied when two bodies hit that don't have defined materials.
123+
* Group.remove has a new 'destroy' parameter (false by default), which will optionally call destroy on the item removed from the Group.
124+
* Group.removeAll has a new 'destroy' parameter (false by default), which will optionally call destroy on the items removed from the Group.
125+
* Group.removeBetween has a new 'destroy' parameter (false by default), which will optionally call destroy on the items removed from the Group.
123126

124127

125128
Bug Fixes
@@ -140,6 +143,7 @@ Bug Fixes
140143
* Emitter.minParticleScale and maxParticleScale wasn't resetting the Body size correctly.
141144
* Group.removeBetween now properly iterates through the children.
142145
* P2.World had a type in the restitution method title. Now fixed.
146+
* Objects with an InputHandler now deactivate it when the object is removed from a Group but not destroyed (fix #672)
143147

144148

145149
p2.js v0.5.0

src/core/Group.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,13 +1394,16 @@ Phaser.Group.prototype.getRandom = function (startIndex, length) {
13941394
*
13951395
* @method Phaser.Group#remove
13961396
* @param {Any} child - The child to remove.
1397+
* @param {boolean} [destroy=false] - You can optionally call destroy on the child that was removed.
13971398
* @return {boolean} true if the child was removed from this Group, otherwise false.
13981399
*/
1399-
Phaser.Group.prototype.remove = function (child) {
1400+
Phaser.Group.prototype.remove = function (child, destroy) {
1401+
1402+
if (typeof destroy === 'undefined') { destroy = false; }
14001403

14011404
if (this.children.length === 0)
14021405
{
1403-
return;
1406+
return false;
14041407
}
14051408

14061409
if (child.events)
@@ -1417,6 +1420,11 @@ Phaser.Group.prototype.remove = function (child) {
14171420
this.next();
14181421
}
14191422

1423+
if (destroy)
1424+
{
1425+
child.destroy();
1426+
}
1427+
14201428
return true;
14211429

14221430
};
@@ -1426,8 +1434,11 @@ Phaser.Group.prototype.remove = function (child) {
14261434
* The Group container remains on the display list.
14271435
*
14281436
* @method Phaser.Group#removeAll
1437+
* @param {boolean} [destroy=false] - You can optionally call destroy on the child that was removed.
14291438
*/
1430-
Phaser.Group.prototype.removeAll = function () {
1439+
Phaser.Group.prototype.removeAll = function (destroy) {
1440+
1441+
if (typeof destroy === 'undefined') { destroy = false; }
14311442

14321443
if (this.children.length === 0)
14331444
{
@@ -1442,6 +1453,11 @@ Phaser.Group.prototype.removeAll = function () {
14421453
}
14431454

14441455
this.removeChild(this.children[0]);
1456+
1457+
if (destroy)
1458+
{
1459+
this.children[0].destroy();
1460+
}
14451461
}
14461462
while (this.children.length > 0);
14471463

@@ -1455,10 +1471,12 @@ Phaser.Group.prototype.removeAll = function () {
14551471
* @method Phaser.Group#removeBetween
14561472
* @param {number} startIndex - The index to start removing children from.
14571473
* @param {number} [endIndex] - The index to stop removing children at. Must be higher than startIndex. If undefined this method will remove all children between startIndex and the end of the Group.
1474+
* @param {boolean} [destroy=false] - You can optionally call destroy on the child that was removed.
14581475
*/
1459-
Phaser.Group.prototype.removeBetween = function (startIndex, endIndex) {
1476+
Phaser.Group.prototype.removeBetween = function (startIndex, endIndex, destroy) {
14601477

14611478
if (typeof endIndex === 'undefined') { endIndex = this.children.length; }
1479+
if (typeof destroy === 'undefined') { destroy = false; }
14621480

14631481
if (this.children.length === 0)
14641482
{
@@ -1481,6 +1499,11 @@ Phaser.Group.prototype.removeBetween = function (startIndex, endIndex) {
14811499

14821500
this.removeChild(this.children[i]);
14831501

1502+
if (destroy)
1503+
{
1504+
this.children[i].destroy();
1505+
}
1506+
14841507
if (this.cursor === this.children[i])
14851508
{
14861509
this.cursor = null;

src/input/InputHandler.js

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,12 @@ Phaser.InputHandler = function (sprite) {
162162
*/
163163
this.consumePointerEvent = false;
164164

165+
/**
166+
* @property {boolean} _wasEnabled - Internal cache var.
167+
* @private
168+
*/
169+
this._wasEnabled = false;
170+
165171
/**
166172
* @property {Phaser.Point} _tempPoint - Internal cache var.
167173
* @private
@@ -235,6 +241,7 @@ Phaser.InputHandler.prototype = {
235241

236242
this.snapOffset = new Phaser.Point();
237243
this.enabled = true;
244+
this._wasEnabled = true;
238245

239246
// Create the signals the Input component will emit
240247
if (this.sprite.events && this.sprite.events.onInputOver === null)
@@ -248,10 +255,48 @@ Phaser.InputHandler.prototype = {
248255
}
249256
}
250257

258+
this.sprite.events.onAddedToGroup.add(this.addedToGroup, this);
259+
this.sprite.events.onRemovedFromGroup.add(this.removedFromGroup, this);
260+
251261
return this.sprite;
252262

253263
},
254264

265+
/**
266+
* Handles when the parent Sprite is added to a new Group.
267+
*
268+
* @method Phaser.InputHandler#addedToGroup
269+
* @private
270+
*/
271+
addedToGroup: function () {
272+
273+
if (this._wasEnabled && !this.enabled)
274+
{
275+
this.start();
276+
}
277+
278+
},
279+
280+
/**
281+
* Handles when the parent Sprite is removed from a Group.
282+
*
283+
* @method Phaser.InputHandler#removedFromGroup
284+
* @private
285+
*/
286+
removedFromGroup: function () {
287+
288+
if (this.enabled)
289+
{
290+
this._wasEnabled = true;
291+
this.stop();
292+
}
293+
else
294+
{
295+
this._wasEnabled = false;
296+
}
297+
298+
},
299+
255300
/**
256301
* Resets the Input Handler and disables it.
257302
* @method Phaser.InputHandler#reset
@@ -553,7 +598,7 @@ Phaser.InputHandler.prototype = {
553598
*/
554599
checkPointerDown: function (pointer) {
555600

556-
if (this.enabled === false || this.sprite.visible === false || this.sprite.parent.visible === false)
601+
if (!this.enabled || !this.sprite || !this.sprite.parent || !this.sprite.visible || !this.sprite.parent.visible)
557602
{
558603
return false;
559604
}
@@ -583,7 +628,7 @@ Phaser.InputHandler.prototype = {
583628
*/
584629
checkPointerOver: function (pointer) {
585630

586-
if (this.enabled === false || this.sprite.visible === false || this.sprite.parent.visible === false)
631+
if (!this.enabled || !this.sprite || !this.sprite.parent || !this.sprite.visible || !this.sprite.parent.visible)
587632
{
588633
return false;
589634
}
@@ -665,7 +710,7 @@ Phaser.InputHandler.prototype = {
665710
*/
666711
update: function (pointer) {
667712

668-
if (this.sprite === null)
713+
if (this.sprite === null || this.sprite.parent === undefined)
669714
{
670715
// Abort. We've been destroyed.
671716
return;

src/physics/p2/Body.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Phaser.Physics.P2.Body = function (game, sprite, x, y, mass) {
5757
* @protected
5858
*/
5959
this.data = new p2.Body({ position: [ this.world.pxmi(x), this.world.pxmi(y) ], mass: mass });
60+
6061
this.data.parent = this;
6162

6263
/**
@@ -74,12 +75,6 @@ Phaser.Physics.P2.Body = function (game, sprite, x, y, mass) {
7475
*/
7576
this.gravity = new Phaser.Point();
7677

77-
/**
78-
* Dispatched when the shape/s of this Body impact with another. The event will be sent 2 parameters, this Body and the impact Body.
79-
* @property {Phaser.Signal} onImpact
80-
*/
81-
this.onImpact = new Phaser.Signal();
82-
8378
/**
8479
* Dispatched when a first contact is created between shapes in two bodies. This event is fired during the step, so collision has already taken place.
8580
* The event will be sent 4 parameters: The body it is in contact with, the shape from this body that caused the contact, the shape from the contact body and the contact equation data array.
@@ -96,7 +91,6 @@ Phaser.Physics.P2.Body = function (game, sprite, x, y, mass) {
9691

9792
/**
9893
* @property {array} collidesWith - Array of CollisionGroups that this Bodies shapes collide with.
99-
* @private
10094
*/
10195
this.collidesWith = [];
10296

@@ -105,6 +99,15 @@ Phaser.Physics.P2.Body = function (game, sprite, x, y, mass) {
10599
*/
106100
this.removeNextStep = false;
107101

102+
/**
103+
* @property {Phaser.Physics.P2.BodyDebug} debugBody - Reference to the debug body.
104+
*/
105+
this.debugBody = null;
106+
107+
/**
108+
* @property {boolean} _collideWorldBounds - Internal var that determines if this Body collides with the world bounds or not.
109+
* @private
110+
*/
108111
this._collideWorldBounds = true;
109112

110113
/**
@@ -131,11 +134,6 @@ Phaser.Physics.P2.Body = function (game, sprite, x, y, mass) {
131134
*/
132135
this._groupCallbackContext = {};
133136

134-
/**
135-
* @property {Phaser.Physics.P2.BodyDebug} debugBody - Reference to the debug body.
136-
*/
137-
this.debugBody = null;
138-
139137
// Set-up the default shape
140138
if (sprite)
141139
{

src/physics/p2/World.js

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Phaser.Physics.P2 = function (game, config) {
2828
}
2929

3030
/**
31-
* @property {p2.World} game - The p2 World in which the simulation is run.
31+
* @property {p2.World} world - The p2 World in which the simulation is run.
3232
* @protected
3333
*/
3434
this.world = new p2.World(config);
@@ -52,7 +52,7 @@ Phaser.Physics.P2 = function (game, config) {
5252
this.materials = [];
5353

5454
/**
55-
* @property {Phaser.InversePointProxy} gravity - The gravity applied to all bodies each step.
55+
* @property {Phaser.Physics.P2.InversePointProxy} gravity - The gravity applied to all bodies each step.
5656
*/
5757
this.gravity = new Phaser.Physics.P2.InversePointProxy(this, this.world.gravity);
5858

@@ -102,16 +102,14 @@ Phaser.Physics.P2 = function (game, config) {
102102
this.onContactMaterialRemoved = new Phaser.Signal();
103103

104104
/**
105-
* @property {Phaser.Signal} onPostBroadphase - Dispatched after the Broadphase has collected collision pairs in the world.
105+
* @property {function} postBroadphaseCallback - A postBroadphase callback.
106106
*/
107107
this.postBroadphaseCallback = null;
108-
this.callbackContext = null;
109108

110109
/**
111-
* @property {Phaser.Signal} onImpact - Dispatched when a first contact is created between two bodies. This event is fired after the step has been done.
110+
* @property {object} callbackContext - The context under which the callbacks are fired.
112111
*/
113-
// this.onImpact = new Phaser.Signal();
114-
this.impactCallback = null;
112+
this.callbackContext = null;
115113

116114
/**
117115
* @property {Phaser.Signal} onBeginContact - Dispatched when a first contact is created between two bodies. This event is fired before the step has been done.
@@ -137,27 +135,42 @@ Phaser.Physics.P2 = function (game, config) {
137135
this.world.on("endContact", this.endContactHandler, this);
138136

139137
/**
140-
* @property {array} _toRemove - Internal var used to hold references to bodies to remove from the world on the next step.
138+
* @property {array} collisionGroups - An array containing the collision groups that have been defined in the World.
141139
*/
142-
this._toRemove = [];
140+
this.collisionGroups = [];
143141

144142
/**
145-
* @property {array} collisionGroups - Internal var.
143+
* @property {Phaser.Physics.P2.CollisionGroup} nothingCollisionGroup - A default collision group.
146144
*/
147-
this.collisionGroups = [];
145+
this.nothingCollisionGroup = new Phaser.Physics.P2.CollisionGroup(1);
148146

149147
/**
150-
* @property {number} _collisionGroupID - Internal var.
151-
* @private
148+
* @property {Phaser.Physics.P2.CollisionGroup} boundsCollisionGroup - A default collision group.
152149
*/
153-
this._collisionGroupID = 2;
154-
155-
this.nothingCollisionGroup = new Phaser.Physics.P2.CollisionGroup(1);
156150
this.boundsCollisionGroup = new Phaser.Physics.P2.CollisionGroup(2);
151+
152+
/**
153+
* @property {Phaser.Physics.P2.CollisionGroup} everythingCollisionGroup - A default collision group.
154+
*/
157155
this.everythingCollisionGroup = new Phaser.Physics.P2.CollisionGroup(2147483648);
158156

157+
/**
158+
* @property {array} boundsCollidesWith - An array of the bodies the world bounds collides with.
159+
*/
159160
this.boundsCollidesWith = [];
160161

162+
/**
163+
* @property {array} _toRemove - Internal var used to hold references to bodies to remove from the world on the next step.
164+
* @private
165+
*/
166+
this._toRemove = [];
167+
168+
/**
169+
* @property {number} _collisionGroupID - Internal var.
170+
* @private
171+
*/
172+
this._collisionGroupID = 2;
173+
161174
// By default we want everything colliding with everything
162175
this.setBoundsToWorld(true, true, true, true, false);
163176

0 commit comments

Comments
 (0)