From 7be11cc54c719ed4afc831c0ff8a8984d98dcef5 Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Fri, 9 May 2014 15:28:50 +0100 Subject: [PATCH] Add bullets. --- images/bullet.png | Bin 0 -> 72 bytes javascripts/game.js | 35 ++++++++++++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 images/bullet.png diff --git a/images/bullet.png b/images/bullet.png new file mode 100644 index 0000000000000000000000000000000000000000..99c1ebc3a4299386a2aa74658852d849f51ee3e1 GIT binary patch literal 72 zcmeAS@N?(olHy`uVBq!ia0vp^EI=&4!3HD^x83^;r1(8u977~7C;#YYlW^O{z|ipX VKVxn4terq922WQ%mvv4FO#mQ15|#h} literal 0 HcmV?d00001 diff --git a/javascripts/game.js b/javascripts/game.js index 3470905..86902e2 100644 --- a/javascripts/game.js +++ b/javascripts/game.js @@ -1,11 +1,12 @@ -var game = new Phaser.Game(1280, 720, Phaser.CANVAS, 'game', { preload: preload, create: create, update: update }); +var game = new Phaser.Game(1280, 720, Phaser.AUTO, 'game', { preload: preload, create: create, update: update }); function preload () { - cursors = game.input.keyboard.createCursorKeys(); - game.load.image('ship', '/images/ship.png'); + game.load.image('bullet', '/images/bullet.png'); } +var bulletTime = 0; + function create () { game.physics.startSystem(Phaser.Physics.ARCADE); @@ -15,10 +16,24 @@ function create () { player.body.bounce.x = 0.5; player.body.collideWorldBounds = true; + + bullets = game.add.group(); + bullets.enableBody = true; + bullets.physicsBodyType = Phaser.Physics.ARCADE; + + // Setup controls + cursors = game.input.keyboard.createCursorKeys(); + fireButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); } function update () { playerMovement(); + + // Firing? + if (fireButton.isDown) { + fireBullet(); + } + } function playerMovement () { @@ -42,3 +57,17 @@ function playerMovement () { } } } + +function fireBullet () { + if (game.time.now > bulletTime) { + var bullet = bullets.create(player.body.x + (player.body.width / 2) - 2, 645, 'bullet'); + bullet.body.velocity.y = -500; + bullet.body.velocity.x = player.body.velocity.x / 4; + bullet.checkWorldBounds = true; + bullet.outOfBoundsKill = true; + // Destroy the bullet when it is killed. + bullet.events.onKilled.add(function() { this.destroy(); }, bullet) + bulletTime = game.time.now + 200; + } + +}