Sherlautten Posted September 23, 2015 Share Posted September 23, 2015 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 More sharing options...
in mono Posted September 23, 2015 Share Posted September 23, 2015 Haven't tried it, but the onBeginContact signal looks like it should do the job for you. Sherlautten 1 Link to comment Share on other sites More sharing options...
Sherlautten Posted September 23, 2015 Author Share Posted September 23, 2015 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){ ball.body.velocity.x=ball.body.velocity.x*0.5; ball.body.velocity.y=ball.body.velocity.y*0.5; if(pushKey.isDown){ var calcX=1; var calcY=1; var referencia=25; if(player.x<ball.x){ calcX=-(player.x-ball.x)/referencia; }else{ calcX=(ball.x-player.x)/referencia; } if(player.y<ball.y){ calcY= -(player.y-ball.y)/referencia; }else{ calcY=(ball.y-player.y)/referencia; } if(player.x===ball.x){ pushX=0; }else if(player.y===ball.y){ pushY=0; } ball.body.velocity.x=pushX*calcX; ball.body.velocity.y=pushY*calcY; }}}; 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 More sharing options...
DonFrag Posted September 23, 2015 Share Posted September 23, 2015 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. Sherlautten 1 Link to comment Share on other sites More sharing options...
Sherlautten Posted September 23, 2015 Author Share Posted September 23, 2015 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! if(distance(player.body.x,player.body.y,ball.body.x,ball.body.y)<25){ shoot(player,ball); } 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 More sharing options...
Recommended Posts