Jump to content

[P2 Physics] Contact events


Recommended Posts

Hi, I'm having some trouble trying to get contact events implemented into my game. All I'm doing right now is creating two sprites (redBlock and greenBlock), enabling p2 physics on each then add the onBeginContact callback which should call the 'blockHit' function when something comes in contact with it.



redBlock.body.onBeginContact.add(this.blockHit, this);


//end of create function.

blockHit: function(body, shapeA, shapeB, equation)

        result = 'You last hit: ' + body.sprite.key;        


 When this function gets called however (which is instantly), the game breaks and the error "Uncaught TypeError: Cannot read property 'sprite' of null." appears. I'm not sure if I've just missed a 'this' or something but any advice would be helpful.


EDIT: After some debugging I've found that the body being passed in is null and that ShapeB is a Plane. As far as I know I'm not adding Plane to any object though.

Link to comment
Share on other sites

Well, I figured out all my problems, at least for me anyway. In the contacts example they use the render function and debug.text, which caused a whole bunch of errors. To fix this I just used a simple text object and setText to change it when blockHit was called. As far as colliding with the plane goes I'm fairly certain that's something to do with colliding with the edges of the world. Gonna try figure out how to stop it checking for the sprite if it collides with the world edge.

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...