Added support to pass 2 array in Arcade collide and overlap (response to an issue).

This commit is contained in:
Carlos Martinez 2014-09-01 14:01:36 +01:00
parent b08bfec372
commit 0640cc0c03

View file

@ -340,16 +340,17 @@ Phaser.Physics.Arcade.prototype = {
* Checks for overlaps between two game objects. The objects can be Sprites, Groups or Emitters.
* You can perform Sprite vs. Sprite, Sprite vs. Group and Group vs. Group overlap checks.
* Unlike collide the objects are NOT automatically separated or have any physics applied, they merely test for overlap results.
* The second parameter can be an array of objects, of differing types.
* Both the first and second parameter can be arrays of objects, of differing types.
* If two arrays are passed, the contents of the first parameter will be tested against all contents of the 2nd parameter.
* NOTE: This function is not recursive, and will not test against children of objects passed (i.e. Groups within Groups).
*
* @method Phaser.Physics.Arcade#overlap
* @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.
* @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.
* @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.
* @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.
* @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.
* @param {object} [callbackContext] - The context in which to run the callbacks.
* @return {boolean} True if an overlap occured otherwise false.
* @return {boolean} True if an overlap occurred otherwise false.
*/
overlap: function (object1, object2, overlapCallback, processCallback, callbackContext) {
@ -360,13 +361,30 @@ Phaser.Physics.Arcade.prototype = {
this._result = false;
this._total = 0;
if (Array.isArray(object2))
if (!Array.isArray(object1) && Array.isArray(object2))
{
for (var i = 0, len = object2.length; i < len; i++)
{
this.collideHandler(object1, object2[i], overlapCallback, processCallback, callbackContext, true);
}
}
else if (Array.isArray(object1) && !Array.isArray(object2))
{
for (var i = 0, len = object1.length; i < len; i++)
{
this.collideHandler(object1[i], object2, overlapCallback, processCallback, callbackContext, true);
}
}
else if (Array.isArray(object1) && Array.isArray(object2))
{
for (var i = 0, len = object1.length; i < len; i++)
{
for (var j = 0, len2 = object2.length; j < len2; j++)
{
this.collideHandler(object1[i], object2[j], overlapCallback, processCallback, callbackContext, true);
}
}
}
else
{
this.collideHandler(object1, object2, overlapCallback, processCallback, callbackContext, true);
@ -378,7 +396,8 @@ Phaser.Physics.Arcade.prototype = {
/**
* 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.
* The second parameter can be an array of objects, of differing types.
* Both the first and second parameter can be arrays of objects, of differing types.
* If two arrays are passed, the contents of the first parameter will be tested against all contents of the 2nd parameter.
* The objects are also automatically separated. If you don't require separation then use ArcadePhysics.overlap instead.
* 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,
* 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.
@ -386,7 +405,7 @@ Phaser.Physics.Arcade.prototype = {
* NOTE: This function is not recursive, and will not test against children of objects passed (i.e. Groups or Tilemaps within other Groups).
*
* @method Phaser.Physics.Arcade#collide
* @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.
* @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.
* @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.
* @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.
* @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 = {
this._result = false;
this._total = 0;
if (Array.isArray(object2))
if (!Array.isArray(object1) && Array.isArray(object2))
{
for (var i = 0, len = object2.length; i < len; i++)
{
this.collideHandler(object1, object2[i], collideCallback, processCallback, callbackContext, false);
}
}
else if (Array.isArray(object1) && !Array.isArray(object2))
{
for (var i = 0, len = object1.length; i < len; i++)
{
this.collideHandler(object1[i], object2, collideCallback, processCallback, callbackContext, false);
}
}
else if (Array.isArray(object1) && Array.isArray(object2))
{
for (var i = 0, len1 = object1.length; i < len1; i++)
{
for (var j = 0, len2 = object2.length; j < len2; j++)
{
this.collideHandler(object1[i], object2[j], collideCallback, processCallback, callbackContext, false);
}
}
}
else
{
this.collideHandler(object1, object2, collideCallback, processCallback, callbackContext, false);