Jump to content

onBeginContact not firing every time


Recommended Posts


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 () {
	if (this.ball.body.velocity.y > 0)
		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:



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

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.

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.


  • Recently Browsing   0 members

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