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

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