Untitled


SUBMITTED BY: Guest

DATE: March 5, 2015, 7:20 a.m.

FORMAT: Text only

SIZE: 9.5 kB

HITS: 653

  1. /**
  2. * Created by kuppa on 4/3/15.
  3. */
  4. var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render });
  5. function preload()
  6. {
  7. game.load.image('ship','invaders/player.png');
  8. game.load.image('bullet','invaders/bullets.png');
  9. game.load.image('invader','invaders/invader32x32x4.png',32,32);
  10. game.load.image('enemyBullet','invaders/enemy-bullet.png');
  11. game.load.image('starfield','invaders/starfield.png');
  12. game.load.image('background','invaders/background2.png');
  13. game.load.spritesheet('kaboom','invaders/explode.png',120,120);
  14. }
  15. var player;
  16. var aliens;
  17. var bullets;
  18. var bulletTime = 0;
  19. var cursors;
  20. var fireButton;
  21. var explosions;
  22. var starfield;
  23. var score = 0;
  24. var scoreString = '';
  25. var scoreText;
  26. var lives;
  27. var enemyBullet;
  28. var firingTimer = 0;
  29. var stateText;
  30. var livingEnemies = [];
  31. function create()
  32. {
  33. game.physics.startSystem(Phaser.Physics.ARCADE);
  34. starfield = game.add.tileSprite(0,0,800,600,'starfield');
  35. bullets = game.add.group();
  36. bullets.enableBody = true;
  37. bullets.physicsBodyType = Phaser.Physics.ARCADE;
  38. bullets.createMultiple(30, 'bullet');
  39. bullets.setAll('anchor.x', 0.5);
  40. bullets.setAll('anchor.y', 1);
  41. bullets.setAll('outOfBoundsKill', true);
  42. bullets.setAll('checkWorldBounds', true);
  43. enemyBullets = game.add.group();
  44. enemyBullets.enableBody = true;
  45. enemyBullets.physicsBodyType = Phaser.Physics.ARCADE;
  46. enemyBullets.createMultiple(30, 'enemyBullet');
  47. enemyBullets.setAll('anchor.x', 0.5);
  48. enemyBullets.setAll('anchor.y', 1);
  49. enemyBullets.setAll('outOfBoundsKill', true);
  50. enemyBullets.setAll('checkWorldBounds', true);
  51. player = game.add.sprite(400, 500, 'ship');
  52. player.anchor.setTo(0.5, 0.5);
  53. game.physics.enable(player, Phaser.Physics.ARCADE);
  54. aliens = game.add.group();
  55. aliens.enableBody = true;
  56. aliens.physicsBodyType = Phaser.Physics.ARCADE;
  57. scoreString = 'Score : ';
  58. scoreText = game.add.text(10, 10, scoreString + score, { font: '34px Arial', fill: '#fff' });
  59. lives = game.add.group();
  60. game.add.text(game.world.width - 100, 10, 'Lives : ', { font: '34px Arial', fill: '#fff' });
  61. stateText = game.add.text(game.world.centerX,game.world.centerY,' ', { font: '84px Arial', fill: '#fff' });
  62. stateText.anchor.setTo(0.5, 0.5);
  63. stateText.visible = false;
  64. stateText = game.add.text(game.world.centerX,game.world.centerY,' ', { font: '84px Arial', fill: '#fff' });
  65. stateText.anchor.setTo(0.5, 0.5);
  66. stateText.visible = false;
  67. for (var i = 0; i < 3; i++)
  68. {
  69. var ship = lives.create(game.world.width - 100 + (30 * i), 60, 'ship');
  70. ship.anchor.setTo(0.5, 0.5);
  71. ship.angle = 90;
  72. ship.alpha = 0.4;
  73. }
  74. explosions = game.add.group();
  75. explosions.createMultiple(30, 'kaboom');
  76. explosions.forEach(setupInvader, this);
  77. cursors = game.input.keyboard.createCursorKeys();
  78. fireButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
  79. function createAliens () {
  80. for (var y = 0; y < 4; y++)
  81. {
  82. for (var x = 0; x < 10; x++)
  83. {
  84. var alien = aliens.create(x * 48, y * 50, 'invader');
  85. alien.anchor.setTo(0.5, 0.5);
  86. alien.animations.add('fly', [ 0, 1, 2, 3 ], 20, true);
  87. alien.play('fly');
  88. alien.body.moves = false;
  89. }
  90. }
  91. aliens.x = 100;
  92. aliens.y = 50;
  93. // All this does is basically start the invaders moving. Notice we're moving the Group they belong to, rather than the invaders directly.
  94. var tween = game.add.tween(aliens).to( { x: 200 }, 2000, Phaser.Easing.Linear.None, true, 0, 1000, true);
  95. // When the tween loops it calls descend
  96. tween.onLoop.add(descend, this);
  97. }
  98. function setupInvader (invader) {
  99. invader.anchor.x = 0.5;
  100. invader.anchor.y = 0.5;
  101. invader.animations.add('kaboom');
  102. }
  103. function descend() {
  104. aliens.y += 10;
  105. }
  106. function update() {
  107. starfield.tilePosition.y += 2;
  108. if (player.alive)
  109. {
  110. // Reset the player, then check for movement keys
  111. player.body.velocity.setTo(0, 0);
  112. if (cursors.left.isDown)
  113. {
  114. player.body.velocity.x = -200;
  115. }
  116. else if (cursors.right.isDown)
  117. {
  118. player.body.velocity.x = 200;
  119. }
  120. // Firing?
  121. if (fireButton.isDown)
  122. {
  123. fireBullet();
  124. }
  125. if (game.time.now > firingTimer)
  126. {
  127. enemyFires();
  128. }
  129. // Run collision
  130. game.physics.arcade.overlap(bullets, aliens, collisionHandler, null, this);
  131. game.physics.arcade.overlap(enemyBullets, player, enemyHitsPlayer, null, this);
  132. }
  133. }
  134. function render() {
  135. for (var i = 0; i < aliens.length; i++)
  136. {
  137. game.debug.body(aliens.children[i]);
  138. }
  139. }
  140. function collisionHandler (bullet, alien) {
  141. // When a bullet hits an alien we kill them both
  142. bullet.kill();
  143. alien.kill();
  144. // Increase the score
  145. score += 20;
  146. scoreText.text = scoreString + score;
  147. // And create an explosion :)
  148. var explosion = explosions.getFirstExists(false);
  149. explosion.reset(alien.body.x, alien.body.y);
  150. explosion.play('kaboom', 30, false, true);
  151. if (aliens.countLiving() == 0)
  152. {
  153. score += 1000;
  154. scoreText.text = scoreString + score;
  155. enemyBullets.callAll('kill',this);
  156. stateText.text = " You Won, \n Click to restart";
  157. stateText.visible = true;
  158. //the "click to restart" handler
  159. game.input.onTap.addOnce(restart,this);
  160. }
  161. }
  162. function enemyHitsPlayer (player,bullet) {
  163. bullet.kill();
  164. live = lives.getFirstAlive();
  165. if (live)
  166. {
  167. live.kill();
  168. }
  169. // And create an explosion :)
  170. var explosion = explosions.getFirstExists(false);
  171. explosion.reset(player.body.x, player.body.y);
  172. explosion.play('kaboom', 30, false, true);
  173. // When the player dies
  174. if (lives.countLiving() < 1)
  175. {
  176. player.kill();
  177. enemyBullets.callAll('kill');
  178. stateText.text=" GAME OVER \n Click to restart";
  179. stateText.visible = true;
  180. //the "click to restart" handler
  181. game.input.onTap.addOnce(restart,this);
  182. }
  183. }
  184. function enemyFires () {
  185. // Grab the first bullet we can from the pool
  186. enemyBullet = enemyBullets.getFirstExists(false);
  187. livingEnemies.length=0;
  188. aliens.forEachAlive(function(alien){
  189. // put every living enemy in an array
  190. livingEnemies.push(alien);
  191. });
  192. if (enemyBullet && livingEnemies.length > 0)
  193. {
  194. var random=game.rnd.integerInRange(0,livingEnemies.length-1);
  195. // randomly select one of them
  196. var shooter=livingEnemies[random];
  197. // And fire the bullet from this enemy
  198. enemyBullet.reset(shooter.body.x, shooter.body.y);
  199. game.physics.arcade.moveToObject(enemyBullet,player,120);
  200. firingTimer = game.time.now + 2000;
  201. }
  202. }
  203. function fireBullet () {
  204. // To avoid them being allowed to fire too fast we set a time limit
  205. if (game.time.now > bulletTime)
  206. {
  207. // Grab the first bullet we can from the pool
  208. bullet = bullets.getFirstExists(false);
  209. if (bullet)
  210. {
  211. // And fire it
  212. bullet.reset(player.x, player.y + 8);
  213. bullet.body.velocity.y = -400;
  214. bulletTime = game.time.now + 200;
  215. }
  216. }
  217. }
  218. function resetBullet (bullet) {
  219. // Called if the bullet goes out of the screen
  220. bullet.kill();
  221. }
  222. function restart () {
  223. // A new level starts
  224. //resets the life count
  225. lives.callAll('revive');
  226. // And brings the aliens back from the dead :)
  227. aliens.removeAll();
  228. createAliens();
  229. //revives the player
  230. player.revive();
  231. //hides the text
  232. stateText.visible = false;
  233. }
  234. }

comments powered by Disqus