Phaser Movement -- Multiple Keys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var game = new Phaser.Game( | |
200, //The width in pixels | |
200, //The height in pixels | |
Phaser.AUTO, //The render system | |
"phaser", //The element | |
{ | |
preload: preload, // The preload function | |
create: create, // The create function | |
update: update // The update function | |
} | |
); | |
// Create the variable outside of any one function scope | |
var sprite; | |
function preload() {} | |
function create() { | |
sprite = game.add.sprite( | |
50, // The x position | |
50, // The y position | |
'sprite' // The image cache-key | |
); | |
} | |
function update() { | |
if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) { | |
sprite.x -= 4; // Move left | |
} | |
if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) { | |
sprite.x += 4; // Move right | |
} | |
if (game.input.keyboard.isDown(Phaser.Keyboard.UP)) { | |
sprite.y -= 4; // Move up | |
} | |
if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN)) { | |
sprite.y += 4; // Move down | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment