# Making my character move right and left with arrow keys?

**URL:** <https://phaser.discourse.group/t/making-my-character-move-right-and-left-with-arrow-keys/929>\
**Category:** Phaser 3\
**Created:** [January 25, 2019, 9:52am UTC](https://phaser.discourse.group/t/making-my-character-move-right-and-left-with-arrow-keys/929 "2019-01-25T09:52:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![philip9](https://avatars.discourse-cdn.com/v4/letter/p/f08c70/32.png) [@philip9](https://phaser.discourse.group/u/philip9)\
**Post date:** [January 25, 2019, 9:52am UTC](https://phaser.discourse.group/t/making-my-character-move-right-and-left-with-arrow-keys/929/1 "2019-01-25T09:52:08Z")

</div>

I am trying to move my spaceship right and left, but it gives me an error: Cannot read property ‘x’ of undefined, somehow my player variable is undefined and I can’t figure out why, can anyone tell me why and how can I fix it so that my ship moves? Here is my code:

var config = {  
type: Phaser.AUTO,  
width: 800,  
height: 700,  
physics: {  
default: ‘arcade’,  
arcade: {  
gravity: {y: 500},  
debug: false  
},  
},  
scene: {  
preload: preload,  
create: create,  
update: update  
},  
};

var game = new Phaser.Game(config);  
var player;  
var background;  
var cursors;

function preload() {  
this.load.image(‘background’, ‘./assets/star-background.jpg’);  
this.load.image(‘player’, ‘./assets/spaceship.png’);  
}

function create() {  
background = this.add.image(400, 300, ‘background’);  
player = this.add.sprite(400,600, ‘player’);  
cursors = this.input.keyboard.createCursorKeys();  
}

function update() {  
if(cursors.right.isDown) {  
player.position.x += 1;  
}  
else if(cursors.left.isDown) {  
player.position.x -= 1;  
}  
}

---

<div class="post-metadata">

**Author:** ![Milton](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@Milton](https://phaser.discourse.group/u/Milton)\
**Post date:** [January 25, 2019, 10:16am UTC](https://phaser.discourse.group/t/making-my-character-move-right-and-left-with-arrow-keys/929/2 "2019-01-25T10:16:30Z")

</div>

Looking at the api I don’t see ‘Sprite.position’. Try setPosition( [x] [, y] [, z] [, w]) or setX().

---

<div class="post-metadata">

**Author:** ![Telinc1](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/telinc1/32/60_2.png) [@Telinc1](https://phaser.discourse.group/u/Telinc1)\
**Post date:** [January 25, 2019, 10:22am UTC](https://phaser.discourse.group/t/making-my-character-move-right-and-left-with-arrow-keys/929/3 "2019-01-25T10:22:03Z")

</div>

You can also directly modify the `x` property of the `player` - `player.x += 1`. Game Objects’ positions aren’t stored as points in Phaser 3; each axis is a separate property.

---

<div class="post-metadata">

**Author:** ![Tonio24100](https://avatars.discourse-cdn.com/v4/letter/t/ec9cab/32.png) [@Tonio24100](https://phaser.discourse.group/u/Tonio24100)\
**Post date:** [January 26, 2019, 6:03am UTC](https://phaser.discourse.group/t/making-my-character-move-right-and-left-with-arrow-keys/929/4 "2019-01-26T06:03:26Z")

</div>

Look at this tuto :

> **[Making your first Phaser 3 game: Part 5 - Ready Player One - Learn - Phaser](https://phaser.io/tutorials/making-your-first-phaser-3-game/part5)**
>
> Desktop and Mobile HTML5 game framework. A fast, free and fun open source framework for Canvas and WebGL powered browser games.

  

> **[Making your first Phaser 3 game: Part 7 - Controlling the player with the...](https://phaser.io/tutorials/making-your-first-phaser-3-game/part7)**
>
> Desktop and Mobile HTML5 game framework. A fast, free and fun open source framework for Canvas and WebGL powered browser games.

Is it helpful ?
