Jump to content

onBeginContact not firing every time


deedeekaka
 Share

Recommended Posts

Hello,

Making a basket ball game and I want to increase the score when I detect that the ball has been sunk in the basket. I place a rectangular sensor body under the net so it doesn't stop the ball from passing through it but can detect collisions. I create the sensor in the create() function:

//Setting up checker physics
this.game.physics.p2.enable(this.checker, true);
this.checker.body.data.shapes[0].sensor = true;
this.checker.body.static = true;

And I add this onBeginContact callback, also in the create() function:

this.checker.body.onBeginContact.add(this.checkIfScored, this);

And since I only want to increase the score if the ball is travelling downwards through the net:

checkIfScored : function () {
		
	console.log(this.ball.body.velocity.y);
	if (this.ball.body.velocity.y > 0)
	{
		console.log("SCORED!");
		this.score++;
		this.scoreText.setText('Score: ' + this.score);
	}
}


My problem is:
onBeginContact is only firing on the ball's way up, but it doesn't fire again on the way down and I don't know why.

You can see the image below of it's setup on the stage.

I've been in and out of the forums, digging for an answer. Any help is very much appreciated!! :lol:

 

issue.jpg

Link to comment
Share on other sites

My guess is it doesn't stop colliding, which is why you never get another onBeginContact signal after the first one. Write a onEndContact to see if thats the case. 

If it is, your object body is probably not placed right. You might also want to enable body.debug to see the body.

Edit, keep in mind these events fire when you collide one of the vertices. Therefore vertice positions are important.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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