Advertisement
Guest User

Won't Restart

a guest
Jan 3rd, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. var Game = {
  2.  
  3. preload : function() {
  4. game.stage.backgroundColor = '#71c5cf';
  5.  
  6. game.load.spritesheet('bee', 'assets/images/sprites/bee-3-sprite.png', 58, 52, 2);
  7. game.load.image('pipe', 'assets/images/pipe.png');
  8.  
  9. // Load the jump sound
  10. game.load.audio('jump', 'assets/images/jump.wav');
  11. game.load.audio('buzzing', 'assets/images/buzzing.wav');
  12. },
  13.  
  14. create : function() {
  15.  
  16. game.physics.startSystem(Phaser.Physics.ARCADE);
  17.  
  18. this.pipes = game.add.group();
  19. this.pipes.enableBody = true;
  20. this.pipes.createMultiple(20, 'pipe');
  21. this.timer = this.game.time.events.loop(1500, this.addRowOfPipes, this);
  22.  
  23. // Add Spritesheet, Animate Spritesheet, Add Physics
  24. this.bee = this.game.add.sprite(100, 245, 'bee');
  25. this.bee.animations.add('fly',[0,1]);
  26. this.bee.animations.play('fly', 20, true);
  27. game.physics.arcade.enable(this.bee);
  28. this.bee.body.gravity.y = 1000;
  29.  
  30. // New anchor position
  31. this.bee.anchor.setTo(-0.2, 0.5);
  32.  
  33. var spaceKey = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
  34. spaceKey.onDown.add(this.jump, this);
  35.  
  36. this.score = 0;
  37. this.labelScore = this.game.add.text(20, 20, "0", { font: "30px Arial", fill: "#ffffff" });
  38.  
  39. // Add the jump sound
  40. this.jumpSound = this.game.add.audio('jump');
  41.  
  42. // Add the buzzin sound
  43. music = game.add.audio('buzzing');
  44.  
  45. music.play();
  46.  
  47. },
  48.  
  49.  
  50.  
  51. update: function() {
  52. if (this.bee.inWorld == false)
  53. this.restartGame();
  54.  
  55. game.physics.arcade.overlap(this.bee, this.pipes, this.hitPipe, null, this);
  56.  
  57. // Slowly rotate the bird downward, up to a certain point.
  58. if (this.bee.angle < 20)
  59. this.bee.angle += 1;
  60. },
  61.  
  62. jump: function() {
  63. // If the bird is dead, he can't jump
  64. if (this.bee.alive == false)
  65. return;
  66.  
  67. this.bee.body.velocity.y = -350;
  68.  
  69. // Jump animation
  70. game.add.tween(this.bee).to({angle: -20}, 100).start();
  71.  
  72. // Play sound
  73. this.jumpSound.play();
  74. },
  75.  
  76. hitPipe: function() {
  77. // If the bird has already hit a pipe, we have nothing to do
  78. if (this.bee.alive == false)
  79. return;
  80.  
  81. // Set the alive property of the bird to false
  82. this.bee.alive = false;
  83.  
  84. // Prevent new pipes from appearing
  85. this.game.time.events.remove(this.timer);
  86.  
  87. // Go through all the pipes, and stop their movement
  88. this.pipes.forEachAlive(function(p){
  89. p.body.velocity.x = 0;
  90. }, this);
  91. },
  92.  
  93. restartGame: function() {
  94. game.state.start('main');
  95. },
  96.  
  97. addOnePipe: function(x, y) {
  98. var pipe = this.pipes.getFirstDead();
  99.  
  100. pipe.reset(x, y);
  101. pipe.body.velocity.x = -200;
  102. pipe.checkWorldBounds = true;
  103. pipe.outOfBoundsKill = true;
  104. },
  105.  
  106. addRowOfPipes: function() {
  107. var hole = Math.floor(Math.random()*5)+1;
  108.  
  109. for (var i = 0; i < 8; i++)
  110. if (i != hole && i != hole +1)
  111. this.addOnePipe(400, i*60+10);
  112.  
  113. this.score += 1;
  114. this.labelScore.text = this.score;
  115. },
  116.  
  117. };
  118.  
  119. game.state.add('main', mainState);
  120. game.state.start('main');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement