Skip to content

Instantly share code, notes, and snippets.

@ada-lovecraft
Last active October 23, 2015 13:28
Show Gist options
  • Select an option

  • Save ada-lovecraft/9901133 to your computer and use it in GitHub Desktop.

Select an option

Save ada-lovecraft/9901133 to your computer and use it in GitHub Desktop.
Phaser 2.0 Tutorial: Flappy Bird (Part 2) : Full tutorial: http://codevinsky.ghost.io/phaser-2-0-tutorial-flappy-bird-part-2/
/* Full tutorial: http://codevinsky.ghost.io/phaser-2-0-tutorial-flappy-bird-part-2/ */
'use strict';
var Bird = function(game, x, y, frame) {
Phaser.Sprite.call(this, game, x, y, 'bird', frame);
this.anchor.setTo(0.5, 0.5);
// add flap animation and begin playing it
this.animations.add('flap');
this.animations.play('flap', 12, true);
// enable physics on the bird
this.game.physics.arcade.enableBody(this);
};
Bird.prototype = Object.create(Phaser.Sprite.prototype);
Bird.prototype.constructor = Bird;
Bird.prototype.update = function() {
};
module.exports = Bird;
/* Full tutorial: http://codevinsky.ghost.io/phaser-2-0-tutorial-flappy-bird-part-2/ */
'use strict';
var Ground = function(game, x, y, width, height) {
Phaser.TileSprite.call(this, game, x, y, width, height, 'ground');
// start scrolling our ground
this.autoScroll(-200,0);
// enable physics on the ground sprite
// this is needed for collision detection
this.game.physics.arcade.enableBody(this);
// we don't want the ground's body
// to be affected by gravity or external forces
this.body.allowGravity = false;
this.body.immovable = true;
};
Ground.prototype = Object.create(Phaser.TileSprite.prototype);
Ground.prototype.constructor = Ground;
Ground.prototype.update = function() {
// write your prefab's specific update code here
};
module.exports = Ground;
/* Full tutorial: http://codevinsky.ghost.io/phaser-2-0-tutorial-flappy-bird-part-2/ */
'use strict';
function Play() {}
Play.prototype = {
create: function() {
// start the phaser arcade physics engine
this.game.physics.startSystem(Phaser.Physics.ARCADE);
// give our world an initial gravity of 500
this.game.physics.arcade.gravity.y = 500;
// add the background sprite
this.background = this.game.add.sprite(0,0,'background');
// create and add a new Bird object
this.bird = new Bird(this.game, 100, this.game.height/2);
this.game.add.existing(this.bird);
// create and add a new Ground object
this.ground = new Ground(this.game, 0, 400, 335, 112);
this.game.add.existing(this.ground);
},
update: function() {
// enable collisions between the bird and the ground
this.game.physics.arcade.collide(this.bird, this.ground);
}
};
module.exports = Play
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment