# Moving with Gravity

**URL:** https://phaser.discourse.group/t/moving-with-gravity/4454
**Category:** Phaser 3
**Created:** [December 3, 2019, 12:49am UTC](https://phaser.discourse.group/t/moving-with-gravity/4454 "2019-12-03T00:49:59Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![LH\_Naseeb](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/lh_naseeb/32/2380_2.png) [@LH\_Naseeb](https://phaser.discourse.group/u/LH_Naseeb)
#### Post date: [December 3, 2019, 12:49am UTC](https://phaser.discourse.group/t/moving-with-gravity/4454/1 "2019-12-03T00:49:59Z")

</div>

Hello! I need help figuring out how to get my sprite to move left and right as it falls because of the built-in gravity. I don’t know why but whenever I try to use the cursor function to move left and right I can’t get any input to the sprite.

```
<script type="text/javascript">
var config = {
    type: Phaser.WEBGL,
    width: 1300,
    height: 600,
    backgroundColor: '#000',
    //parent: 'phaser-example',
    physics: {
        default: 'arcade',
        
            arcade: {
            gravity: { y: 300 },
            debug: true
        }
        
    },
    scene: {
        preload: preload,
        create: create
    }
};

var game = new Phaser.Game(config);

function preload ()
{
    this.load.image('bg', 'assets/background.png');
    this.load.image('star', 'assets/sprites/peonay.png');
}

function create ()
{
    //camera and world bounds
    this.cameras.main.setBounds(0, 0, 1000 , 1200 );
    this.physics.world.setBounds(220, 0, 850 , 1200);

    //adding the background
    this.add.image(250, 0, 'bg').setOrigin(0);

    player = this.physics.add.sprite(650, 0, 'star');

    player.setCollideWorldBounds(true);

    this.cameras.main.startFollow(player, true, 0.05, 0.05);

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

function update () {
    if (cursors.left.isDown)
    {
        player.setVelocityX(-500);
    }
    else if (cursors.right.isDown)
    {
        player.setVelocityX(500);
    }

}

</script>

</body>
</html>
```

---

<div class="post-metadata">

### Author: ![doreancl](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/doreancl/32/2297_2.png) [@doreancl](https://phaser.discourse.group/u/doreancl)
#### Post date: [December 3, 2019, 1:16am UTC](https://phaser.discourse.group/t/moving-with-gravity/4454/2 "2019-12-03T01:16:04Z")

</div>

You can check in the update function if the actual player.x is bigger than the last frame or lower and play the respective animation

---

<div class="post-metadata">

### Author: ![LH\_Naseeb](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/lh_naseeb/32/2380_2.png) [@LH\_Naseeb](https://phaser.discourse.group/u/LH_Naseeb)
#### Post date: [December 3, 2019, 1:21am UTC](https://phaser.discourse.group/t/moving-with-gravity/4454/3 "2019-12-03T01:21:41Z")

</div>

I’m not quite sure what you mean. The star at the moment doesn’t move at all. I’m just trying to get the star to fall while also being able to move it left and right.

---

<div class="post-metadata">

### Author: ![smjn](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/smjn/32/2087_2.png) [@smjn](https://phaser.discourse.group/u/smjn)
#### Post date: [December 3, 2019, 6:50am UTC](https://phaser.discourse.group/t/moving-with-gravity/4454/4 "2019-12-03T06:50:13Z")

</div>

If this is the whole code, then only the create function knows there is player. You have to make the player var global or part of scene class (using this).

From memory I think the value of gravity is much lower, more around 1 and not 300.

---

<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: [December 3, 2019, 8:49am UTC](https://phaser.discourse.group/t/moving-with-gravity/4454/5 "2019-12-03T08:49:57Z")

</div>

Your `update()` function isn’t getting run. It needs to be added to the scene config.

```javascript
scene: {
  preload: preload,
  create: create,
  update: update
}

```
