Skip to content

Instantly share code, notes, and snippets.

Last active January 3, 2016 04:19
What would you like to do?
Update description of BasicPlatformer
function update() {
// Using the physics.arcade system, check if 'player' is colliding
// with any tiles within 'layer'. If so, seperate them.
game.physics.arcade.collide(player, layer);
// Reset the x (horizontal) velocity
player.body.velocity.x = 0;
// Check if the left arrow key is being pressed
if (cursors.left.isDown)
// Set the 'player' sprite's x velocity to a negative number:
// have it move left on the screen.
player.body.velocity.x = -hozMove;
// Check if 'facing' is not "left"
if (facing !== "left")
// Set 'facing' to "left"
facing = "left";
// Check if the right arrow key is being pressed
else if (cursors.right.isDown)
// Set the 'player' sprite's x velocity to a positive number:
// have it move right on the screen.
player.body.velocity.x = hozMove;
// Check if 'facing' is not "right"
if (facing !== "right")
// Set 'facing' to "right"
facing = "right";
// Check if the jumpButton (SPACEBAR) is down AND
// if the 'player' physics body is onFloor (touching a tile) AND
// if the current game.time is greater than the value of 'jumpTimer'
// (Here, we need to make sure the player cannot jump while alreay in the air
// AND that jumping takes place while the sprite is colliding with
// a tile in order to jump off it.)
if (jumpButton.isDown && player.body.onFloor() && > jumpTimer)
// Set the 'player' sprite's y velocity to a negative number
// (vertMove is -90) and thus have it move up on the screen.
player.body.velocity.y = vertMove;
// Add 650 and the current time together and set that value to 'jumpTimer'
// (The 'jumpTimer' is how long in milliseconds between jumps.
// Here, that is 650 ms.)
jumpTimer = + 650;
// Check if 'facing' is "left"
if (facing === "left") {
// Set the 'player' to the second (1) frame
// ('facing' is "left")
player.frame = 1;
} else {
// Set the 'player' to the first (0) frame
// ('facing' is "right").
player.frame = 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment