Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var Game = {
- preload : function() {
- game.stage.backgroundColor = '#71c5cf';
- game.load.spritesheet('bee', 'assets/images/sprites/bee-3-sprite.png', 58, 52, 2);
- game.load.image('pipe', 'assets/images/pipe.png');
- // Load the jump sound
- game.load.audio('jump', 'assets/images/jump.wav');
- game.load.audio('buzzing', 'assets/images/buzzing.wav');
- },
- create : function() {
- game.physics.startSystem(Phaser.Physics.ARCADE);
- this.pipes = game.add.group();
- this.pipes.enableBody = true;
- this.pipes.createMultiple(20, 'pipe');
- this.timer = this.game.time.events.loop(1500, this.addRowOfPipes, this);
- // Add Spritesheet, Animate Spritesheet, Add Physics
- this.bee = this.game.add.sprite(100, 245, 'bee');
- this.bee.animations.add('fly',[0,1]);
- this.bee.animations.play('fly', 20, true);
- game.physics.arcade.enable(this.bee);
- this.bee.body.gravity.y = 1000;
- // New anchor position
- this.bee.anchor.setTo(-0.2, 0.5);
- var spaceKey = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
- spaceKey.onDown.add(this.jump, this);
- this.score = 0;
- this.labelScore = this.game.add.text(20, 20, "0", { font: "30px Arial", fill: "#ffffff" });
- // Add the jump sound
- this.jumpSound = this.game.add.audio('jump');
- // Add the buzzin sound
- music = game.add.audio('buzzing');
- music.play();
- },
- update: function() {
- if (this.bee.inWorld == false)
- this.restartGame();
- game.physics.arcade.overlap(this.bee, this.pipes, this.hitPipe, null, this);
- // Slowly rotate the bird downward, up to a certain point.
- if (this.bee.angle < 20)
- this.bee.angle += 1;
- },
- jump: function() {
- // If the bird is dead, he can't jump
- if (this.bee.alive == false)
- return;
- this.bee.body.velocity.y = -350;
- // Jump animation
- game.add.tween(this.bee).to({angle: -20}, 100).start();
- // Play sound
- this.jumpSound.play();
- },
- hitPipe: function() {
- // If the bird has already hit a pipe, we have nothing to do
- if (this.bee.alive == false)
- return;
- // Set the alive property of the bird to false
- this.bee.alive = false;
- // Prevent new pipes from appearing
- this.game.time.events.remove(this.timer);
- // Go through all the pipes, and stop their movement
- this.pipes.forEachAlive(function(p){
- p.body.velocity.x = 0;
- }, this);
- },
- restartGame: function() {
- game.state.start('main');
- },
- addOnePipe: function(x, y) {
- var pipe = this.pipes.getFirstDead();
- pipe.reset(x, y);
- pipe.body.velocity.x = -200;
- pipe.checkWorldBounds = true;
- pipe.outOfBoundsKill = true;
- },
- addRowOfPipes: function() {
- var hole = Math.floor(Math.random()*5)+1;
- for (var i = 0; i < 8; i++)
- if (i != hole && i != hole +1)
- this.addOnePipe(400, i*60+10);
- this.score += 1;
- this.labelScore.text = this.score;
- },
- };
- game.state.add('main', mainState);
- game.state.start('main');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement