Jump to content

[P2 Physics] Contact events


Nechro
 Share

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

 Share

  • Recently Browsing   0 members

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