Jump to content

How to get a P2 object to ignore gravity and maintain its y position?


iambaz
 Share

Recommended Posts

I'm trying to make a game which has balls dropping from the top of the screen and then a hand catches them at the bottom. The hand is a sprite which I have made a P2 object as I will need to detect collisions to see if a ball has been caught.

My problem is that no matter what I try, the hand object just flys around the screen on initialisation as if there is already a force applied to it. Here is my code:

function create(){

    game.physics.startSystem(Phaser.Physics.P2JS);

    hand = game.add.sprite(0,750,"hand");
    hand.scale.setTo(0.5);
    hand.anchor.setTo(0.5, 1);
    hand.x = game.width / 2;
    game.physics.p2.enable(hand);
    hand.body.data.gravityScale = 0;
    hand.body.setZeroVelocity();

}

So what happens when I execute this is the hand starts at the 750 pixel vertical and in the middle (as expected) but immediately flys up to the top of the screen and then hits the top and slowly starts making its way back down. 

I don't want it to move, I just want it to stay at 750px vertical! 

What am I missing?

Thanks

Link to comment
Share on other sites

Ok, I see what is happening. 750 was actually out of the stage bounds, so the physics engine was 'bouncing' it as if it had hit the screen's edge. By changing the value to 650px vertical so all of the object's bounds are on the stage, I get the static y axis I wanted. 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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