Jump to content

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


BdR
 Share

Recommended Posts

See fiddle here:
https://jsfiddle.net/krrgLkqL/

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.anchor.setTo(0.5);
    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

 Share

  • Recently Browsing   0 members

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