Jump to content

Why does allowGravity() affect all sprites?


Recommended Posts

I have two sprites `bunny` and `baddy`. I want gravity to enact on `bunny` but not `baddy`.

I understand that I can set `baddy.body.allowGravity()` to false but when I do this, gravity stops enacting on `bunny` too. I have discovered that it happens no matter whether I pass `true` or `false` to `allowGravity()` -- the result is always that gravity stops enacting on both sprites.

function create() {
    // create the player sprite
    bunny = this.physics.add.sprite(200, 200, 'bunny');
    bunny.body.setBounce(0.2); // our player will bounce from items
    bunny.body.setCollideWorldBounds(true); // don't go out of the map

    // create the baddy sprite
    baddy = this.physics.add.sprite(500,200, 'baddy');
    baddy.body.allowGravity(true); // This isn't working as I expect it to!!

    cursors = this.input.keyboard.createCursorKeys();

Am I doing anything obviously wrong? How do I prevent gravity enacting on `baddy` but not `bunny`?

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.

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.


  • Recently Browsing   0 members

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