Skip to content

Commit af09290

Browse files
committed
Add getObjectsAtLocation method in Arcade physics.
1 parent 8d2cb71 commit af09290

1 file changed

Lines changed: 23 additions & 4 deletions

File tree

src/physics/arcade/World.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,32 +1407,51 @@ Phaser.Physics.Arcade.prototype = {
14071407
return;
14081408
}
14091409

1410+
return this.getObjectsAtLocation(pointer.x, pointer.y, group, callback, callbackContext, pointer);
1411+
1412+
},
1413+
1414+
/**
1415+
* Given a Group and a location this will check to see which Group children overlap with the coordinates.
1416+
* Each child will be sent to the given callback for further processing.
1417+
* Note that the children are not checked for depth order, but simply if they overlap the coordinate or not.
1418+
*
1419+
* @method Phaser.Physics.Arcade#getObjectsAtLocation
1420+
* @param {Phaser.Pointer} pointer - The Pointer to check.
1421+
* @param {Phaser.Group} group - The Group to check.
1422+
* @param {function} [callback] - A callback function that is called if the object overlaps the coordinates. The callback will be sent two parameters: the callbackArg and the Object that overlapped the location.
1423+
* @param {object} [callbackContext] - The context in which to run the callback.
1424+
* @param {object} [callbackArg] - An argument to pass to the callback.
1425+
* @return {array} An array of the Sprites from the Group that overlapped the coordinates.
1426+
*/
1427+
getObjectsAtLocation: function (x, y, group, callback, callbackContext, callbackArg) {
1428+
14101429
this.quadTree.clear();
14111430

14121431
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);
14131432

14141433
this.quadTree.populate(group);
14151434

1416-
var rect = new Phaser.Rectangle(pointer.x, pointer.y, 1, 1);
1435+
var rect = new Phaser.Rectangle(x, y, 1, 1);
14171436
var output = [];
14181437

14191438
this._potentials = this.quadTree.retrieve(rect);
14201439

14211440
for (var i = 0, len = this._potentials.length; i < len; i++)
14221441
{
1423-
if (this._potentials[i].hitTest(pointer.x, pointer.y))
1442+
if (this._potentials[i].hitTest(x, y))
14241443
{
14251444
if (callback)
14261445
{
1427-
callback.call(callbackContext, pointer, this._potentials[i].sprite);
1446+
callback.call(callbackContext, callbackArg, this._potentials[i].sprite);
14281447
}
14291448

14301449
output.push(this._potentials[i].sprite);
14311450
}
14321451
}
14331452

14341453
return output;
1435-
1454+
14361455
},
14371456

14381457
/**

0 commit comments

Comments
 (0)