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

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.

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

Loading...
 Share

  • Recently Browsing   0 members

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