# Sprite's body position not consistent during animation

**URL:** <https://phaser.discourse.group/t/sprites-body-position-not-consistent-during-animation/5242>\
**Category:** Phaser 3\
**Created:** [February 18, 2020, 3:25am UTC](https://phaser.discourse.group/t/sprites-body-position-not-consistent-during-animation/5242 "2020-02-18T03:25:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gagg910](https://avatars.discourse-cdn.com/v4/letter/g/848f3c/32.png) [@gagg910](https://phaser.discourse.group/u/gagg910)\
**Post date:** [February 18, 2020, 3:25am UTC](https://phaser.discourse.group/t/sprites-body-position-not-consistent-during-animation/5242/1 "2020-02-18T03:25:11Z")

</div>

![animsandbody](https://global.discourse-cdn.com/free1/uploads/phaser1/original/2X/5/582e2fb9a14ffb129de4de4fb1869b14c5deb7c0.png)

As visible from the picture, I am struggling to force the gameobject’s body to stay in position, specially when performing the animation from stab, significantly wider than the default (bottom-left corner) after having flipped left. This is a case of the body shifting left, as the animation stays in place and it does not overflow to the right as it could be misunderstood from the picture.  
A possible workaround I imagine is to set the body offset an amount of pixels to the right during that particular animation, however I feel reluctant to do this as it seems ‘hacky’.

As I’m new to the framework this is the bare bones code to make the man move and stab so far.

```
let stabOnCooldown = false;
let onStab = false;
function create ()
{
    player= this.physics.add.sprite(500, 500, 'animation-default').setScale(2).setSize(37, 64, false);
    //animations, keys
}
function moveRight() {
    player.setFlip(false, false);
    player.setVelocityX(speed);
    player.anims.play('walk', true);        
}
function moveLeft() {
    player.setFlip(true, false);
    player.setVelocityX(-speed);
    player.anims.play('walk', true);
}
function stop() {
    player.setVelocityX(0)
    player.anims.play('still',true);
}

function update ()
{
    //Perform a stab if 'x' is pressed
    if (attackKey.X.isDown && !stabOnCooldown) {
        onStab = true;
        stabOnCooldown = true;
        player.setVelocityX(0);
        player.anims.play('stab', true);
       //stab cooldown logic: perform stab 1/4 of a second and disable it for half a second afterwards
        setTimeout(function() {
            onStab = false;
            setTimeout(function() {
                stabOnCooldown = false;
            }, 500);
        }, 250);
    }
    else if (horizontalMoveKeys.RIGHT.isDown && !onStab) {moveRight();}
    else if (horizontalMoveKeys.LEFT.isDown && !onStab) {moveLeft();}
    else if (!onStab) {stop();}
}
```

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [February 18, 2020, 6:22pm UTC](https://phaser.discourse.group/t/sprites-body-position-not-consistent-during-animation/5242/2 "2020-02-18T18:22:38Z")

</div>

If the texture frames are different sizes then you probably have to resize or offset the body to align it with the frame.
