Skip to content

Commit 03a2db1

Browse files
committed
Added extra protection in the case of mixed-type Groups.
Removed window vars and replaced with stats property. Removed redundant if/else checks.
1 parent 8aec760 commit 03a2db1

1 file changed

Lines changed: 58 additions & 33 deletions

File tree

src/physics/arcade/World.js

Lines changed: 58 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ Phaser.Physics.Arcade = function (game) {
7575
*/
7676
this.quadTree = new Phaser.QuadTree(this.game.world.bounds.x, this.game.world.bounds.y, this.game.world.bounds.width, this.game.world.bounds.height, this.maxObjects, this.maxLevels);
7777

78+
/**
79+
* @property {object} stats - Stats collected for each collision iteration.
80+
*/
81+
this.stats = { 'skipped': 0, 'ignored': 0, 'checked': 0 };
82+
7883
/**
7984
* @property {number} _total - Internal cache var.
8085
* @private
@@ -415,34 +420,71 @@ Phaser.Physics.Arcade.prototype = {
415420

416421
},
417422

423+
/**
424+
* This method will sort a Groups _hash array based on the sortDirection property.
425+
*
426+
* Each function should return -1 if `a > b`, 1 if `a < b` or 0 if `a === b`.
427+
*
428+
* @method sort
429+
* @protected
430+
* @param {Phaser.Group} group - The Group to sort.
431+
*/
418432
sort: function (group) {
419433

420434
if (this.sortDirection === Phaser.Physics.Arcade.LEFT_RIGHT)
421435
{
422436
// Game world is say 2000x600 and you start at 0
423437
group._hash.sort(function(a, b) {
438+
439+
if (!a.body || !b.body)
440+
{
441+
return -1;
442+
}
443+
424444
return a.body.x - b.body.x;
445+
425446
});
426447
}
427448
else if (this.sortDirection === Phaser.Physics.Arcade.RIGHT_LEFT)
428449
{
429450
// Game world is say 2000x600 and you start at 2000
430451
group._hash.sort(function(a, b) {
452+
453+
if (!a.body || !b.body)
454+
{
455+
return -1;
456+
}
457+
431458
return b.body.x - a.body.x;
459+
432460
});
433461
}
434462
else if (this.sortDirection === Phaser.Physics.Arcade.TOP_BOTTOM)
435463
{
436464
// Game world is say 800x2000 and you start at 0
437465
group._hash.sort(function(a, b) {
466+
467+
if (!a.body || !b.body)
468+
{
469+
return -1;
470+
}
471+
438472
return a.body.y - b.body.y;
473+
439474
});
440475
}
441476
else if (this.sortDirection === Phaser.Physics.Arcade.BOTTOM_TOP)
442477
{
443478
// Game world is say 800x2000 and you start at 2000
444479
group._hash.sort(function(a, b) {
480+
481+
if (!a.body || !b.body)
482+
{
483+
return -1;
484+
}
485+
445486
return b.body.y - a.body.y;
487+
446488
});
447489
}
448490

@@ -601,14 +643,14 @@ Phaser.Physics.Arcade.prototype = {
601643

602644
if (this.skipQuadTree || sprite.body.skipQuadTree)
603645
{
604-
window.skipped = 0;
605-
window.ignored = 0;
606-
window.checked = 0;
646+
this.stats.skipped = 0;
647+
this.stats.ignored = 0;
648+
this.stats.checked = 0;
607649

608650
for (var i = 0; i < group._hash.length; i++)
609651
{
610-
// Skip duff entries
611-
if (!group._hash[i] || !group._hash[i].exists)
652+
// Skip duff entries - we can't check a non-existent sprite or one with no body
653+
if (!group._hash[i] || !group._hash[i].exists || !group._hash[i].body)
612654
{
613655
continue;
614656
}
@@ -618,74 +660,57 @@ Phaser.Physics.Arcade.prototype = {
618660
{
619661
if (group._hash[i].body.right < sprite.body.x)
620662
{
621-
window.ignored++;
663+
this.stats.ignored++;
622664
continue;
623665
}
624666
else if (sprite.body.right < group._hash[i].body.x)
625667
{
626-
window.skipped = group._hash.length - i;
668+
this.stats.skipped = group._hash.length - i;
627669
break;
628670
}
629-
else
630-
{
631-
window.checked++;
632-
this.collideSpriteVsSprite(sprite, group._hash[i], collideCallback, processCallback, callbackContext, overlapOnly);
633-
}
634671
}
635672
else if (this.sortDirection === Phaser.Physics.Arcade.RIGHT_LEFT)
636673
{
637674
if (group._hash[i].body.x > sprite.body.right)
638675
{
639-
window.ignored++;
676+
this.stats.ignored++;
640677
continue;
641678
}
642679
else if (sprite.body.x > group._hash[i].body.right)
643680
{
644-
window.skipped = group._hash.length - i;
681+
this.stats.skipped = group._hash.length - i;
645682
break;
646683
}
647-
else
648-
{
649-
window.checked++;
650-
this.collideSpriteVsSprite(sprite, group._hash[i], collideCallback, processCallback, callbackContext, overlapOnly);
651-
}
652684
}
653685
else if (this.sortDirection === Phaser.Physics.Arcade.TOP_BOTTOM)
654686
{
655687
if (group._hash[i].body.bottom < sprite.body.y)
656688
{
657-
window.ignored++;
689+
this.stats.ignored++;
658690
continue;
659691
}
660692
else if (sprite.body.bottom < group._hash[i].body.y)
661693
{
662-
window.skipped = group._hash.length - i;
694+
this.stats.skipped = group._hash.length - i;
663695
break;
664696
}
665-
else
666-
{
667-
window.checked++;
668-
this.collideSpriteVsSprite(sprite, group._hash[i], collideCallback, processCallback, callbackContext, overlapOnly);
669-
}
670697
}
671698
else if (this.sortDirection === Phaser.Physics.Arcade.BOTTOM_TOP)
672699
{
673700
if (group._hash[i].body.y > sprite.body.bottom)
674701
{
675-
window.ignored++;
702+
this.stats.ignored++;
676703
continue;
677704
}
678705
else if (sprite.body.y > group._hash[i].body.bottom)
679706
{
680-
window.skipped = group._hash.length - i;
707+
this.stats.skipped = group._hash.length - i;
681708
break;
682709
}
683-
else
684-
{
685-
window.checked++;
686-
this.collideSpriteVsSprite(sprite, group._hash[i], collideCallback, processCallback, callbackContext, overlapOnly);
687-
}
688710
}
711+
712+
this.stats.checked++;
713+
this.collideSpriteVsSprite(sprite, group._hash[i], collideCallback, processCallback, callbackContext, overlapOnly);
689714
}
690715
}
691716
else

0 commit comments

Comments
 (0)