1
0
mirror of https://github.com/danbee/invaders synced 2025-03-04 08:39:08 +00:00

Add bullets.

This commit is contained in:
Dan Barber 2014-05-09 15:28:50 +01:00
parent 9f9c3d3132
commit 7be11cc54c
2 changed files with 32 additions and 3 deletions

BIN
images/bullet.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 72 B

View File

@ -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;
}
}