Created
April 14, 2015 09:46
-
-
Save thaisingle/c9fa47d31023c31e57af to your computer and use it in GitHub Desktop.
How to create Flappy Bird (Part 8) – Jumping sound
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(400, 490, Phaser.AUTO, 'flappyDiv'); | |
var mainState = { | |
preload: function() { | |
game.stage.backgroundColor = '#71c5cf'; | |
game.load.image('bird', 'assets/bird.png'); | |
game.load.image('pipe', 'assets/pipe.png'); | |
game.load.audio('jump', 'assets/flap.wav'); | |
}, | |
create: function() { | |
this.bird = this.game.add.sprite(100, 245, 'bird'); | |
game.physics.startSystem(Phaser.Physics.ARCADE); | |
game.physics.arcade.enable(this.bird); | |
this.bird.body.gravity.y = 1000; | |
var spaceKey = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); | |
spaceKey.onDown.add(this.jump, this); | |
this.pipes = game.add.group(); | |
this.pipes.enableBody = true; | |
this.pipes.createMultiple(20, 'pipe'); | |
this.timer = game.time.events.loop(1600, this.addLongPipes, this); | |
this.score = 0; | |
this.labelScore = game.add.text(20, 20, "0", { font: "30px Arial", fill: "#ffffff" }); | |
this.bird.anchor.setTo(-0.2, 0.5); | |
this.jumpSound = game.add.audio('jump'); | |
}, | |
update: function() { | |
game.physics.arcade.overlap(this.bird, this.pipes, this.overlapPipe, null, this); | |
if (this.bird.inWorld == false){ | |
this.restartGame(); | |
} | |
if (this.bird.angle < 20) { | |
this.bird.angle += 1; | |
} | |
}, | |
overlapPipe: function() { | |
game.time.events.remove(this.timer); | |
this.pipes.forEachAlive(function(p){ | |
p.body.velocity.x = 0; | |
}, this); | |
this.bird.alive = false; | |
}, | |
jump: function() { | |
if (this.bird.alive == false) { | |
return; | |
} | |
this.bird.body.velocity.y = -350; | |
var animation = game.add.tween(this.bird); | |
animation.to({angle: -20}, 100); | |
animation.start(); | |
this.jumpSound.play(); | |
}, | |
restartGame: function() { | |
game.state.start('main'); | |
}, | |
addShotPipe: function(x, y) { | |
var pipe = this.pipes.getFirstDead(); | |
pipe.reset(x, y); | |
pipe.body.velocity.x = -200; | |
pipe.checkWorldBounds = true; | |
pipe.outOfBoundsKill = true; | |
}, | |
addLongPipes: function() { | |
var blank = Math.floor(Math.random() * 5) + 1; | |
for (var i = 0; i < 8; i++){ | |
if (i != blank && i != blank + 1){ | |
this.addShotPipe(400, (i * 60) + 10); | |
} | |
} | |
this.score += 1; | |
this.labelScore.text = this.score; | |
}, | |
}; | |
game.state.add('main', mainState); | |
game.state.start('main'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment