Skip to content

Commit 2b40c8a

Browse files
committed
Pointer.type and Pointer.exists properties added.
QuadTree.retrieve can now accept either a Sprite with a physics body or a Phaser.Rectangle as its parameter. ArcadePhysics.getObjectsUnderPointer will return all children from a Group that overlap with the given Pointer.
1 parent ad7e2af commit 2b40c8a

5 files changed

Lines changed: 85 additions & 12 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ Version 2.0.5 - "Tanchico" - in development
6262
* TypeScript definitions fixes and updates (thanks @luispedrofonseca @clark-stevenson)
6363
* Input.getPointerFromIdentifier docs update to reflect where the identifier comes from. Pointer properties now set to give it fixed defaults (thanks @JirkaDellOro, #793)
6464
* Pointer.pointerId added which is set by the DOM event (if present in the browser). Note that browsers can and do recycle pointer IDs.
65+
* Pointer.type and Pointer.exists properties added.
66+
* QuadTree.retrieve can now accept either a Sprite with a physics body or a Phaser.Rectangle as its parameter.
6567

6668

6769
### New Features
@@ -70,11 +72,13 @@ Version 2.0.5 - "Tanchico" - in development
7072
* Group.hasProperty will check a child for the given property and return a boolean.
7173
* Phaser.Tween.from allows you to set tween properties that will end up where the current object is (thanks @codevinsky, #792)
7274
* Input.getPointerFromId will return a pointer with a matching pointerId value, if any. pointerId is a value set by the browser in the DOM event.
75+
* ArcadePhysics.getObjectsUnderPointer will return all children from a Group that overlap with the given Pointer.
7376

7477

7578
### Bug Fixes
7679

7780
* Line.pointOnLine corrected algorithm (thanks @woutercommandeur, fix #784)
81+
* Line segment collision fails under certain cicumstances (thanks @woutercommandeur, fix #760)
7882
* The P2 DistanceConstraint method signature has changed. Updated Phaser so maxForce is now passed as object (fix #788)
7983
* Moved the this._reversed flag outside of the property loop in Tween (as per tween.js issue 115)
8084
* Emitter.makeParticles updated to use Array.isArray() check on the key/frame values, so non-string objects can be passed in (thanks @AnderbergE, fix #786)

src/Phaser.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ var Phaser = Phaser || {
4343
ELLIPSE: 16,
4444
SPRITEBATCH: 17,
4545
RETROFONT: 18,
46+
POINTER: 19,
4647

4748
// The various blend modes supported by pixi / phaser
4849
blendModes: {

src/input/Pointer.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ Phaser.Pointer = function (game, id) {
2525
*/
2626
this.id = id;
2727

28+
/**
29+
* @property {number} type - The const type of this object.
30+
* @readonly
31+
*/
32+
this.type = Phaser.POINTER;
33+
34+
/**
35+
* @property {boolean} exists - A Pointer object that exists is allowed to be checked for physics collisions and overlaps.
36+
* @default
37+
*/
38+
this.exists = true;
39+
2840
/**
2941
* @property {number} identifier - The identifier property of the Pointer as set by the DOM event when this Pointer is started.
3042
* @default

src/math/QuadTree.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -284,37 +284,46 @@ Phaser.QuadTree.prototype = {
284284
},
285285

286286
/**
287-
* Return all objects that could collide with the given Sprite.
287+
* Return all objects that could collide with the given Sprite or Rectangle.
288288
*
289289
* @method Phaser.QuadTree#retrieve
290-
* @param {Phaser.Sprite} sprite - The sprite to check against.
290+
* @param {Phaser.Sprite|Phaser.Rectangle} source - The source object to check the QuadTree against. Either a Sprite or Rectangle.
291291
* @return {array} - Array with all detected objects.
292292
*/
293-
retrieve: function (sprite) {
293+
retrieve: function (source) {
294294

295-
if (!sprite.body)
295+
if (source instanceof Phaser.Rectangle)
296296
{
297-
return this._empty;
297+
var returnObjects = this.objects;
298+
299+
var index = this.getIndex(source);
298300
}
301+
else
302+
{
303+
if (!source.body)
304+
{
305+
return this._empty;
306+
}
299307

300-
var returnObjects = this.objects;
308+
var returnObjects = this.objects;
301309

302-
var index = this.getIndex(sprite.body);
310+
var index = this.getIndex(source.body);
311+
}
303312

304313
if (this.nodes[0])
305314
{
306315
// If rect fits into a subnode ..
307316
if (index !== -1)
308317
{
309-
returnObjects = returnObjects.concat(this.nodes[index].retrieve(sprite));
318+
returnObjects = returnObjects.concat(this.nodes[index].retrieve(source));
310319
}
311320
else
312321
{
313322
// If rect does not fit into a subnode, check it against all subnodes (unrolled for speed)
314-
returnObjects = returnObjects.concat(this.nodes[0].retrieve(sprite));
315-
returnObjects = returnObjects.concat(this.nodes[1].retrieve(sprite));
316-
returnObjects = returnObjects.concat(this.nodes[2].retrieve(sprite));
317-
returnObjects = returnObjects.concat(this.nodes[3].retrieve(sprite));
323+
returnObjects = returnObjects.concat(this.nodes[0].retrieve(source));
324+
returnObjects = returnObjects.concat(this.nodes[1].retrieve(source));
325+
returnObjects = returnObjects.concat(this.nodes[2].retrieve(source));
326+
returnObjects = returnObjects.concat(this.nodes[3].retrieve(source));
318327
}
319328
}
320329

src/physics/arcade/World.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,6 +1318,53 @@ Phaser.Physics.Arcade.prototype = {
13181318

13191319
},
13201320

1321+
/**
1322+
* Given a Group and a Pointer this will check to see which Group children overlap with the Pointer coordinates.
1323+
* Each child will be sent to the given callback for further processing.
1324+
* Note that the children are not checked for depth order, but simply if they overlap the Pointer or not.
1325+
*
1326+
* @method Phaser.Physics.Arcade#getObjectsUnderPointer
1327+
* @param {Phaser.Pointer} pointer - The Pointer to check.
1328+
* @param {Phaser.Group} group - The Group to check.
1329+
* @param {function} [callback] - A callback function that is called if the object overlaps with the Pointer. The callback will be sent two parameters: the Pointer and the Object that overlapped with it.
1330+
* @param {object} [callbackContext] - The context in which to run the callback.
1331+
* @return {array} An array of the Sprites from the Group that overlapped the Pointer coordinates.
1332+
*/
1333+
getObjectsUnderPointer: function (pointer, group, callback, callbackContext) {
1334+
1335+
if (group.length === 0 || !pointer.exists)
1336+
{
1337+
return;
1338+
}
1339+
1340+
this.quadTree.clear();
1341+
1342+
this.quadTree.reset(this.game.world.bounds.x, this.game.world.bounds.y, this.game.world.bounds.width, this.game.world.bounds.height, this.maxObjects, this.maxLevels);
1343+
1344+
this.quadTree.populate(group);
1345+
1346+
var rect = new Phaser.Rectangle(pointer.x, pointer.y, 1, 1);
1347+
var output = [];
1348+
1349+
this._potentials = this.quadTree.retrieve(rect);
1350+
1351+
for (var i = 0, len = this._potentials.length; i < len; i++)
1352+
{
1353+
if (this._potentials[i].hitTest(pointer.x, pointer.y))
1354+
{
1355+
if (callback)
1356+
{
1357+
callback.call(callbackContext, pointer, this._potentials[i].sprite);
1358+
}
1359+
1360+
output.push(this._potentials[i].sprite);
1361+
}
1362+
}
1363+
1364+
return output;
1365+
1366+
},
1367+
13211368
/**
13221369
* Move the given display object towards the destination object at a steady velocity.
13231370
* If you specify a maxTime then it will adjust the speed (overwriting what you set) so it arrives at the destination in that number of seconds.

0 commit comments

Comments
 (0)