2013-10-22 02:58:20 +00:00
|
|
|
|
|
|
|
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update });
|
|
|
|
|
|
|
|
function preload() {
|
|
|
|
|
|
|
|
game.load.image('phaser', 'assets/sprites/phaser-dude.png');
|
2013-11-01 17:28:09 +00:00
|
|
|
game.load.image('bullet', 'assets/misc/bullet0.png');
|
2013-10-22 02:58:20 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-11-01 17:28:09 +00:00
|
|
|
var sprite;
|
|
|
|
var bullet;
|
|
|
|
var bullets;
|
|
|
|
var bulletTime = 0;
|
|
|
|
|
2013-10-22 02:58:20 +00:00
|
|
|
function create() {
|
|
|
|
|
2013-11-01 17:28:09 +00:00
|
|
|
game.stage.backgroundColor = '#2d2d2d';
|
|
|
|
|
|
|
|
bullets = game.add.group();
|
|
|
|
bullets.createMultiple(10, 'bullet');
|
|
|
|
bullets.callAll('events.onOutOfBounds.add', 'events.onOutOfBounds', resetBullet, this);
|
2013-10-22 02:58:20 +00:00
|
|
|
|
2013-11-01 17:28:09 +00:00
|
|
|
sprite = game.add.sprite(400, 550, 'phaser');
|
|
|
|
|
|
|
|
// Stop the following keys from propagating up to the browser
|
|
|
|
game.input.keyboard.addKeyCapture([ Phaser.Keyboard.LEFT, Phaser.Keyboard.RIGHT, Phaser.Keyboard.SPACEBAR ]);
|
2013-10-22 02:58:20 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function update() {
|
|
|
|
|
2013-11-01 17:28:09 +00:00
|
|
|
sprite.body.velocity.x = 0;
|
|
|
|
sprite.body.velocity.y = 0;
|
|
|
|
|
|
|
|
if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
|
2013-10-22 02:58:20 +00:00
|
|
|
{
|
2013-11-01 17:28:09 +00:00
|
|
|
sprite.body.velocity.x = -200;
|
|
|
|
}
|
|
|
|
else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
|
|
|
|
{
|
|
|
|
sprite.body.velocity.x = 200;
|
2013-10-22 02:58:20 +00:00
|
|
|
}
|
|
|
|
|
2013-11-01 17:28:09 +00:00
|
|
|
if (game.input.keyboard.isDown(Phaser.Keyboard.SPACEBAR))
|
2013-10-22 02:58:20 +00:00
|
|
|
{
|
2013-11-01 17:28:09 +00:00
|
|
|
fireBullet();
|
2013-10-22 02:58:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2013-11-01 17:28:09 +00:00
|
|
|
|
|
|
|
function fireBullet () {
|
|
|
|
|
|
|
|
if (game.time.now > bulletTime)
|
|
|
|
{
|
|
|
|
bullet = bullets.getFirstExists(false);
|
|
|
|
|
|
|
|
if (bullet)
|
|
|
|
{
|
|
|
|
bullet.reset(sprite.x + 6, sprite.y - 8);
|
|
|
|
bullet.body.velocity.y = -300;
|
|
|
|
bulletTime = game.time.now + 250;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called if the bullet goes out of the screen
|
|
|
|
function resetBullet (bullet) {
|
|
|
|
bullet.kill();
|
|
|
|
}
|
|
|
|
|