Jump to content

There is it some "While contact" event?


Recommended Posts

Hi, I'm having a trouble in Phaser, using P2 physics.


  ball.body.createBodyCallback(player,shoot, this);    



Actually the method calls the shoot function, which pushes the ball if I'm pressing Space in the keyboard, but it only pushes the ball if it's pressed before the two bodys touch.


I would like to be able to shoot the ball at any time when the bodys are touching, even when I'm moving the ball with the player and the bodys are touching, as if it was a football ball in our feet.


Do you know any method? I have tried all the examples and till now I couldn't be able to fix it. :(


Ty all!!

Link to comment
Share on other sites

Haven't tried it, but the onBeginContact signal looks like it should do the job for you.

I tried that this way, 



   ball.body.onBeginContact.add(shoot, this);   




function shoot(body1, body2){

        var calcX=1;
        var calcY=1;
        var referencia=25;
           calcY= -(player.y-ball.y)/referencia;
        }else if(player.y===ball.y){


But it still doesn't works, I have the same problem, once they are in contact, shoot never stars, maybe the problem is in my own function, but I don't see where it is, maybe I should change the pushkey isdown for another, but I don't know which one is the correct.

Link to comment
Share on other sites

once they are in contact you can measure the distance between.Once that distance is > your MAX_DISTANCE stop checking distance (a bool) and stop the firing.


Wonderful, thank you very much!



function distance (x1, y1, x2, y2) {
        var dx = x1 - x2;
        var dy = y1 - y2;
        return Math.sqrt(dx * dx + dy * dy);


It works perfectly now, THANKS AGAIN!

Link to comment
Share on other sites


  • Recently Browsing   0 members

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