Skip to content

Instantly share code, notes, and snippets.

@ada-lovecraft
Last active September 27, 2015 14:44
Show Gist options
  • Save ada-lovecraft/9816091 to your computer and use it in GitHub Desktop.
Save ada-lovecraft/9816091 to your computer and use it in GitHub Desktop.
Phaser 2.0 Tutorial: Flappy Bird (Part 1) : Full tutorial: http://codevinsky.ghost.io/phaser-2-0-tutorial-flappy-bird-part-1/
/* Full tutorial: http://codevinsky.ghost.io/phaser-2-0-tutorial-flappy-bird-part-1/ */
'use strict';
function Menu() {}
Menu.prototype = {
preload: function() {
},
create: function() {
// add the background sprite
this.background = this.game.add.sprite(0,0,'background');
// add the ground sprite as a tile
// and start scrolling in the negative x direction
this.ground = this.game.add.tileSprite(0,400, 335,112,'ground');
this.ground.autoScroll(-200,0);
/** STEP 1 **/
// create a group to put the title assets in
// so they can be manipulated as a whole
this.titleGroup = this.game.add.group()
/** STEP 2 **/
// create the title sprite
// and add it to the group
this.title = this.add.sprite(0,0,'title');
this.titleGroup.add(this.title);
/** STEP 3 **/
// create the bird sprite
// and add it to the title group
this.bird = this.add.sprite(200,5,'bird');
this.titleGroup.add(this.bird);
/** STEP 4 **/
// add an animation to the bird
// and begin the animation
this.bird.animations.add('flap');
this.bird.animations.play('flap', 12, true);
/** STEP 5 **/
// Set the originating location of the group
this.titleGroup.x = 30;
this.titleGroup.y = 100;
/** STEP 6 **/
// create an oscillating animation tween for the group
this.game.add.tween(this.titleGroup).to({y:115}, 350, Phaser.Easing.Linear.NONE, true, 0, 1000, true);
// add our start button with a callback
this.startButton = this.game.add.button(this.game.width/2, 300, 'startButton', this.startClick, this);
this.startButton.anchor.setTo(0.5,0.5);
},
startClick: function() {
// start button click handler
// start the 'play' state
this.game.state.start('play');
}
};
module.exports = Menu;
/* Full tutorial: http://codevinsky.ghost.io/phaser-2-0-tutorial-flappy-bird-part-1/ */
'use strict';
function Preload() {
this.asset = null;
this.ready = false;
}
Preload.prototype = {
preload: function() {
this.asset = this.add.sprite(this.width/2,this.height/2, 'preloader');
this.asset.anchor.setTo(0.5, 0.5);
this.load.onLoadComplete.addOnce(this.onLoadComplete, this);
this.load.setPreloadSprite(this.asset);
this.load.image('background', 'assets/background.png');
this.load.image('ground', 'assets/ground.png');
this.load.image('title', 'assets/title.png');
this.load.spritesheet('bird', 'assets/bird.png', 34,24,3);
this.load.image('startButton', 'assets/start-button.png');
},
create: function() {
this.asset.cropEnabled = false;
},
update: function() {
if(!!this.ready) {
this.game.state.start('menu');
}
},
onLoadComplete: function() {
this.ready = true;
}
};
module.exports = Preload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment