Skip to content

Instantly share code, notes, and snippets.

@brett-richardson
Created September 8, 2016 20:05
Show Gist options
  • Save brett-richardson/1cb05b8f63cb04ba89ad905e7f287eea to your computer and use it in GitHub Desktop.
Save brett-richardson/1cb05b8f63cb04ba89ad905e7f287eea to your computer and use it in GitHub Desktop.
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('space', 'assets/skies/deep-space.jpg');
game.load.image('bullet', 'assets/games/asteroids/bullets.png');
game.load.image('ship', 'assets/games/asteroids/ship.png');
}
var sprite;
var cursors;
var emitter;
var bullet;
var bullets;
var bulletTime = 0;
function create() {
// This will run in Canvas mode, so let's gain a little speed and display
game.renderer.clearBeforeRender = false;
game.renderer.roundPixels = true;
// We need arcade physics
game.physics.startSystem(Phaser.Physics.ARCADE);
// A spacey background
game.add.tileSprite(0, 0, game.width, game.height, 'space');
// Our ships bullets
bullets = game.add.group();
bullets.enableBody = true;
bullets.physicsBodyType = Phaser.Physics.ARCADE;
// All 40 of them
bullets.createMultiple(40, 'bullet');
bullets.setAll('anchor.x', 0.5);
bullets.setAll('anchor.y', 0.5);
// Our player ship
sprite = game.add.sprite(300, 300, 'ship');
sprite.anchor.set(0.5);
// and its physics settings
game.physics.enable(sprite, Phaser.Physics.ARCADE);
sprite.body.drag.set(100);
sprite.body.maxVelocity.set(200);
console.log("player", sprite.body);
emitter = game.add.emitter(game.world.centerX, game.world.centerY, 250);
emitter.makeParticles('ship', [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20], 200, true, true);
emitter.start(false, 8000, 400);
console.log("emitter", emitter);
// Game input
cursors = game.input.keyboard.createCursorKeys();
game.input.keyboard.addKeyCapture([ Phaser.Keyboard.SPACEBAR ]);
}
function update() {
if (cursors.up.isDown){
game.physics.arcade.accelerationFromRotation(sprite.rotation, 200, sprite.body.acceleration);
} else {
sprite.body.acceleration.set(0);
}
if (cursors.left.isDown) {
sprite.body.angularVelocity = -300;
} else if (cursors.right.isDown) {
sprite.body.angularVelocity = 300;
} else {
sprite.body.angularVelocity = 0;
}
if (game.input.keyboard.isDown(Phaser.Keyboard.SPACEBAR)) {
fireBullet();
}
screenWrap(sprite);
bullets.forEachExists(screenWrap, this);
game.physics.arcade.collide(emitter, sprite);
}
function fireBullet () {
if (game.time.now > bulletTime) {
bullet = bullets.getFirstExists(false);
if (bullet) {
bullet.reset(sprite.body.x + 16, sprite.body.y + 16);
bullet.lifespan = 2000;
bullet.rotation = sprite.rotation;
game.physics.arcade.velocityFromRotation(sprite.rotation, 400, bullet.body.velocity);
bulletTime = game.time.now + 50;
}
}
}
function screenWrap (sprite) {
if (sprite.x < 0) {
sprite.x = game.width;
} else if (sprite.x > game.width) {
sprite.x = 0;
}
if (sprite.y < 0) {
sprite.y = game.height;
} else if (sprite.y > game.height) {
sprite.y = 0;
}
}
function render() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment