Skip to content

Commit 895ab12

Browse files
committed
Merge pull request phaserjs#1158 from ctmartinez1992/dev
Added support to pass 2 array in Arcade collide and overlap
2 parents 58b1409 + ec9a8b6 commit 895ab12

3 files changed

Lines changed: 46 additions & 10 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ If you need to support IE9 or Android 2.x and want to use P2 physics then you mu
268268

269269
Phaser is developed in JavaScript. We've made no assumptions about how you like to code your games, and were careful not to impose any form of class / inheritance / structure upon you. So you won't find it split into require modules or pull in 3rd party npm packages for example. That doesn't mean you can't, it just means we don't force you to do so. If you're a requireJS user you'll find a new template in the `resources\Project Templates` folder just for you.
270270

271-
If you code with [TypeScript](https://typescript.codeplex.com/) you'll find a comprehensive definitions file inside the `build` folder and tutorials on getting started.
271+
If you code with [TypeScript](http://www.typescriptlang.org/) you'll find a comprehensive definitions file inside the `build` folder and tutorials on getting started.
272272

273273
Phaser is 128 KB gzipped (576 KB minified) when including all 3 physics engines. Without the physics engines its 67 KB gzipped (311 KB minified)
274274

src/core/Camera.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ Phaser.Camera.prototype = {
227227
this.displayObject.position.x = -this.view.x;
228228
this.displayObject.position.y = -this.view.y;
229229

230+
this.view.floor();
231+
230232
},
231233

232234
/**
@@ -315,8 +317,6 @@ Phaser.Camera.prototype = {
315317
this.view.y = this.bounds.bottom - this.height;
316318
}
317319

318-
this.view.floor();
319-
320320
},
321321

322322
/**

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)