Skip to content

Commit 0640cc0

Browse files
Added support to pass 2 array in Arcade collide and overlap (response to an issue).
1 parent b08bfec commit 0640cc0

1 file changed

Lines changed: 43 additions & 7 deletions

File tree

src/physics/arcade/World.js

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -340,16 +340,17 @@ Phaser.Physics.Arcade.prototype = {
340340
* Checks for overlaps between two game objects. The objects can be Sprites, Groups or Emitters.
341341
* You can perform Sprite vs. Sprite, Sprite vs. Group and Group vs. Group overlap checks.
342342
* Unlike collide the objects are NOT automatically separated or have any physics applied, they merely test for overlap results.
343-
* The second parameter can be an array of objects, of differing types.
343+
* Both the first and second parameter can be arrays of objects, of differing types.
344+
* If two arrays are passed, the contents of the first parameter will be tested against all contents of the 2nd parameter.
344345
* NOTE: This function is not recursive, and will not test against children of objects passed (i.e. Groups within Groups).
345346
*
346347
* @method Phaser.Physics.Arcade#overlap
347-
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter} object1 - The first object to check. Can be an instance of Phaser.Sprite, Phaser.Group or Phaser.Particles.Emitter.
348+
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|array} object1 - The first object or array of objects to check. Can be Phaser.Sprite, Phaser.Group or Phaser.Particles.Emitter.
348349
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|array} object2 - The second object or array of objects to check. Can be Phaser.Sprite, Phaser.Group or Phaser.Particles.Emitter.
349350
* @param {function} [overlapCallback=null] - An optional callback function that is called if the objects overlap. The two objects will be passed to this function in the same order in which you specified them.
350351
* @param {function} [processCallback=null] - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then overlapCallback will only be called if processCallback returns true.
351352
* @param {object} [callbackContext] - The context in which to run the callbacks.
352-
* @return {boolean} True if an overlap occured otherwise false.
353+
* @return {boolean} True if an overlap occurred otherwise false.
353354
*/
354355
overlap: function (object1, object2, overlapCallback, processCallback, callbackContext) {
355356

@@ -360,13 +361,30 @@ Phaser.Physics.Arcade.prototype = {
360361
this._result = false;
361362
this._total = 0;
362363

363-
if (Array.isArray(object2))
364+
if (!Array.isArray(object1) && Array.isArray(object2))
364365
{
365366
for (var i = 0, len = object2.length; i < len; i++)
366367
{
367368
this.collideHandler(object1, object2[i], overlapCallback, processCallback, callbackContext, true);
368369
}
369370
}
371+
else if (Array.isArray(object1) && !Array.isArray(object2))
372+
{
373+
for (var i = 0, len = object1.length; i < len; i++)
374+
{
375+
this.collideHandler(object1[i], object2, overlapCallback, processCallback, callbackContext, true);
376+
}
377+
}
378+
else if (Array.isArray(object1) && Array.isArray(object2))
379+
{
380+
for (var i = 0, len = object1.length; i < len; i++)
381+
{
382+
for (var j = 0, len2 = object2.length; j < len2; j++)
383+
{
384+
this.collideHandler(object1[i], object2[j], overlapCallback, processCallback, callbackContext, true);
385+
}
386+
}
387+
}
370388
else
371389
{
372390
this.collideHandler(object1, object2, overlapCallback, processCallback, callbackContext, true);
@@ -378,15 +396,16 @@ Phaser.Physics.Arcade.prototype = {
378396

379397
/**
380398
* Checks for collision between two game objects. You can perform Sprite vs. Sprite, Sprite vs. Group, Group vs. Group, Sprite vs. Tilemap Layer or Group vs. Tilemap Layer collisions.
381-
* The second parameter can be an array of objects, of differing types.
399+
* Both the first and second parameter can be arrays of objects, of differing types.
400+
* If two arrays are passed, the contents of the first parameter will be tested against all contents of the 2nd parameter.
382401
* The objects are also automatically separated. If you don't require separation then use ArcadePhysics.overlap instead.
383402
* An optional processCallback can be provided. If given this function will be called when two sprites are found to be colliding. It is called before any separation takes place,
384403
* giving you the chance to perform additional checks. If the function returns true then the collision and separation is carried out. If it returns false it is skipped.
385404
* The collideCallback is an optional function that is only called if two sprites collide. If a processCallback has been set then it needs to return true for collideCallback to be called.
386405
* NOTE: This function is not recursive, and will not test against children of objects passed (i.e. Groups or Tilemaps within other Groups).
387406
*
388407
* @method Phaser.Physics.Arcade#collide
389-
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|Phaser.Tilemap} object1 - The first object to check. Can be an instance of Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter, or Phaser.Tilemap.
408+
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|Phaser.Tilemap|array} object1 - The first object or array of objects to check. Can be Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter, or Phaser.Tilemap.
390409
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|Phaser.Tilemap|array} object2 - The second object or array of objects to check. Can be Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter or Phaser.Tilemap.
391410
* @param {function} [collideCallback=null] - An optional callback function that is called if the objects collide. The two objects will be passed to this function in the same order in which you specified them, unless you are colliding Group vs. Sprite, in which case Sprite will always be the first parameter.
392411
* @param {function} [processCallback=null] - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then collision will only happen if processCallback returns true. The two objects will be passed to this function in the same order in which you specified them.
@@ -402,13 +421,30 @@ Phaser.Physics.Arcade.prototype = {
402421
this._result = false;
403422
this._total = 0;
404423

405-
if (Array.isArray(object2))
424+
if (!Array.isArray(object1) && Array.isArray(object2))
406425
{
407426
for (var i = 0, len = object2.length; i < len; i++)
408427
{
409428
this.collideHandler(object1, object2[i], collideCallback, processCallback, callbackContext, false);
410429
}
411430
}
431+
else if (Array.isArray(object1) && !Array.isArray(object2))
432+
{
433+
for (var i = 0, len = object1.length; i < len; i++)
434+
{
435+
this.collideHandler(object1[i], object2, collideCallback, processCallback, callbackContext, false);
436+
}
437+
}
438+
else if (Array.isArray(object1) && Array.isArray(object2))
439+
{
440+
for (var i = 0, len1 = object1.length; i < len1; i++)
441+
{
442+
for (var j = 0, len2 = object2.length; j < len2; j++)
443+
{
444+
this.collideHandler(object1[i], object2[j], collideCallback, processCallback, callbackContext, false);
445+
}
446+
}
447+
}
412448
else
413449
{
414450
this.collideHandler(object1, object2, collideCallback, processCallback, callbackContext, false);

0 commit comments

Comments
 (0)