-
-
Save ylluminarious/0c14293e4b41cf41d4da to your computer and use it in GitHub Desktop.
Updated source code for example on StackOverflow and the Phaser forum
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(800, 600, Phaser.CANVAS, "game", {preload: preload, create: create, update: update}); | |
var dude; | |
var block; | |
var spacebar; | |
var gameOverText; | |
var gameOverCounter = 0; | |
var gameOverBool = false; | |
var downCounter = 0; | |
function preload () { | |
game.load.image("block", "assets/block.png"); | |
game.load.image("dude", "assets/phaser-dude.png"); | |
} | |
function create () { | |
dude = game.add.sprite(373, 760, "dude"); | |
block = game.add.sprite(0, 505, "block"); | |
game.physics.arcade.enable(dude); | |
game.physics.arcade.enable(block); | |
dude.body.collideWorldBounds = true; | |
dude.body.gravity.y = 200; | |
block.body.collideWorldBounds = true; | |
block.body.immovable = true; | |
block.body.velocity.x = 100; | |
block.body.bounce.x = 1; | |
var jumpOrTryAgain = function () { | |
if (gameOverBool === false) { | |
dude.body.velocity.y = -250; | |
console.log("down"); | |
} else { | |
dude.destroy(); | |
block.destroy(); | |
gameOverText.destroy(); | |
gameOverBool = false; | |
gameOverCounter = 0; | |
create(); | |
} | |
}; | |
if (downCounter === 0) { | |
spacebar = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); | |
game.input.onDown.add(jumpOrTryAgain); | |
spacebar.onDown.add(jumpOrTryAgain); | |
downCounter += 1; | |
} | |
} | |
function update () { | |
function gameOver () { | |
if (gameOverCounter === 0) { | |
gameOverBool = true; | |
dude.body.velocity.y = 0; | |
dude.body.velocity.x = 0; | |
block.body.velocity.x = 0; | |
gameOverText = game.add.text(300, 200, "Game Over!", {fontSize: "16px", fill: "white"}); | |
gameOverCounter += 1; | |
} | |
} | |
game.physics.arcade.collide(dude, block, gameOver); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment