Object positioning after FlipX for non-centered sprite

Hi guys,

I’m new to phaser 3 and have a problem with my hero when flipping the X axis.

I’ve attached a video to illustrate my problem:
flipproblem

The corresponding sprite is not centered:
GraveRobber

The defined offset and size:

this.setOrigin(0.5,1)
this.body.setSize(12,33)
this.body.setOffset(9,15)

When I hit the left key:

this.body.offset.x = 26

And when i hit the right key again, it resets to the default offset of 9:

this.body.offset.x = 9

How can I maintain the “correct” positioning during/after the flip?

Thx!
Cheers,
Chris

1 Like

:wave:

Can you describe the problem as it appears in the video?

Sure. When I hit the “left” key, the hero turns left as intended. However the hero is now displaced by ~25 pixels.

1 Like

I added this code in the preUpdate(time, delta) of the Hero class extending from Phaser.GameObjects.Sprite

if(!this.isDead() && this.keys.left.isDown) {
  this.body.setAccelerationX(-500)
  this.setFlipX(true)
  this.body.offset.x = 26
} else if (!this.isDead() && this.keys.right.isDown) {
  this.body.setAccelerationX(500)
  this.setFlipX(false)
  this.body.offset.x = 9
} else {
  this.body.setAccelerationX(0)
}

Playing around with this.body.x -= 17; didn’t seem to have any effects

I think you must adjust sprite x (in preUpdate()) or sprite.body.x (in scene update()) when the sprite flipX changes. But it seems awkward.