Jump to content

[SOLVED] Issue looping through two groups with non-physics based collision.


Epicshark
 Share

Recommended Posts

I been having an issue while looping through 2 groups and got this error, what I think this means is one of the objects is undefined.

Uncaught TypeError: Cannot read property 'width' of undefined

This is code (which is in Typescript) that causes the error:

for (var i = this.tris.children.length - 1; i >= 0; i--) {
	for (var s = this.squares.children.length - 1; s >= 0; s--) {
		if(Overlap(this.tris.children[i],this.squares.children[s])){
			this.BreakTris(this.tris.children[i]);
			this.BreakSquare(this.squares.children[s]);
		}
	}
}

The error is given specifically for the Overlap function. Two loops which go through 2 groups in order to compare each sprite of both groups to each other and check for Overlap, which is as follows:

function Overlap(spriteA, spriteB) {

    var boundsA = (spriteA instanceof Phaser.Sprite) ? spriteA.getBounds() : spriteA;
	var boundsB = (spriteB instanceof Phaser.Sprite) ? spriteB.getBounds() : spriteB;

    return Phaser.Rectangle.intersects(boundsA, boundsB);
}

I don't see what i'm doing wrong? Thanks for reading.

Link to comment
Share on other sites

I'm not sure entirely what I did but I solved the issue with this code:

for (var i = 0; i < this.tris.children.length; i++) {
	for (var s = 0; s < this.squares.children.length; s++) {
		if (this.tris.children[i] != null && this.squares.children[s] != null){
			if(Overlap(this.tris.children[i],this.squares.children[s])){
				this.BreakTris(this.tris.children[i]);
				this.BreakSquare(this.squares.children[s]);
			}
		}
	}
}

I solved it by checking for if the sprites were null before overlap.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...