You may have noticed that it is quite easy to jump over the blocks if you just hold down the mouse long enough to jump all the way to the top of the game! So to make it more of a challenge the first thing I thought was to add a bird in the sky. Sometimes you’ll have to jump over the bird and sometimes jump between the bird and the blocks.<\/p>\n
For now, we will just use a blue block to represent the bird.<\/p>\n
<\/p>\n
You can download that image or download the zip file at the end of this post.<\/p>\n
Let’s start by preloading the bird graphic. Place this in the stateMain.js preload function<\/p>\n
game.load.image(\"bird\", \"images\/bird.png\");<\/pre>\nNext, we will use a function to create the bird, and we will only have one bird on the screen at a time.<\/p>\n
makeBird: function() {\r\n \/\/if the bird already exists \r\n \/\/destroy it\r\n if (this.bird) {\r\n this.bird.destroy();\r\n }\r\n \/\/pick a number at the top of the screen\r\n \/\/between 10 percent and 40 percent of the height of the screen\r\n var birdY = game.rnd.integerInRange(game.height * .1, game.height * .4);\r\n \/\/add the bird sprite to the game\r\n this.bird = game.add.sprite(game.width + 100, birdY, \"bird\");\r\n \/\/enable the sprite for physics\r\n game.physics.enable(this.bird, Phaser.Physics.ARCADE);\r\n \/\/set the x velocity at -200 which is a little faster than the blocks\r\n this.bird.body.velocity.x = -200;\r\n \/\/set the bounce for the bird\r\n this.bird.body.bounce.set(2, 2);\r\n }<\/pre>\nIn the update function, we need to check to see if the bird has flown off the screen<\/p>\n
\/\/if the bird has flown off screen\r\n\/\/reset it\r\nif (this.bird.x < 0) {\r\n this.makeBird();\r\n}<\/pre>\nAdding the game over state<\/h2>\n