Jump to content

Sprite scale.x to flip left and right, body not aligned


Recommended Posts

See fiddle here:

I'm working on a game with tilemaps and arcade physics etc. and I'm running into an issue. I've got a sprite sheet of a running guy and the physics body rectangle is adjusted, so the physics.body only covers the torso of the sprite guy.

    player.body.setSize(30, 56, 10, -3); // width, height, offsetX, offsetY

The sprite is always facing right and when the player moves to the left, I just flip the scale.x to -1 So something like this:

    if (_cursors.right.isDown && player.body.velocity.x >= 0) {
        // move right
        player.body.velocity.x = MOVE_SPEED;
        player.scale.setTo(1, 1); // flip to right
    } else if (_cursors.left.isDown && player.body.velocity.x <= 0) {
        // move left
        player.body.velocity.x = -1*MOVE_SPEED;
        player.scale.setTo(-1, 1); // flip to left

The problem is that when the player faces left, the body is outside of the sprite, the body and the sprite don't overlap anymore which screws up the collision detection.

Any idea how to solve this? :wacko:

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