mirror of
https://github.com/danbee/invaders
synced 2025-03-04 08:39:08 +00:00
Add bullets.
This commit is contained in:
parent
9f9c3d3132
commit
7be11cc54c
BIN
images/bullet.png
Normal file
BIN
images/bullet.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 72 B |
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user