Epicshark

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

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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.