Jump to content

Contact/Collides event on P2


Sherlautten
 Share

Recommended Posts

Hi, I'm working on P2 physics and my idea is to have a player who controls the ball and moves with it, and when he wants, he can shoot the ball, but I've got two problems... 

 

Actually I'm using a ContactCallback function, where if I push the SPACEBAR, it kicks the ball.

 

 

function create(){   
 
ball.body.setBodyContactCallback(player,chutar, this);    
}

 

 

 

function chutar(body1, body2){

 

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;
 
    }
}

 

 

The problem I've got is that the ball is kicked only when I touch the ball while pressing SPACEBAR. That's a possibility which I also want, but I also want to control the ball and once they are in contact, be able to shoot, or maybe keep controlling, 

 

Actually I have to separate the player of the ball to be able to shoot :/ I want to shoot it every time I press the SPACEBAR and they are touching, I don't know if the problem is in the "ContactCallBack "method or maybe on the  key.isDown.

 

I tried to use key.onDown, but I had another problem, the first time works correctly, but after it I could kick the ball from anywhere even If I wasn't touching it  :huh:  :huh: .

 

Any idea how to fix it???

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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