var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update }); function preload() { game.load.image('bullet', 'assets/games/invaders/bullet.png'); game.load.image('enemyBullet', 'assets/games/invaders/enemy-bullet.png'); game.load.spritesheet('invader', 'assets/games/invaders/invader32x32x4.png', 32, 32); game.load.image('ship', 'assets/games/invaders/player.png'); game.load.spritesheet('kaboom', 'assets/games/invaders/explode.png', 128, 128); game.load.image('starfield', 'assets/games/invaders/starfield.png'); game.load.image('background', 'assets/games/starstruck/background2.png'); } var player; var aliens; var bullets; var bulletTime = 0; var cursors; var fireButton; var explosions; var starfield; function create() { // The scrolling starfield background starfield = game.add.tileSprite(0, 0, 800, 600, 'starfield'); // game.add.tileSprite(0, 0, 800, 600, 'background'); // Our bullet group bullets = game.add.group(); bullets.createMultiple(30, 'bullet'); bullets.setAll('anchor.x', 0.5); bullets.setAll('anchor.y', 1); bullets.setAll('outOfBoundsKill', true); // The hero! player = game.add.sprite(400, 500, 'ship'); player.anchor.setTo(0.5, 0.5); // The baddies! aliens = game.add.group(); for (var y = 0; y < 4; y++) { for (var x = 0; x < 10; x++) { var alien = aliens.create(x * 48, y * 50, 'invader'); alien.animations.add('fly', [0,1,2,3], 20, true); alien.play('fly'); } } aliens.x = 100; aliens.y = 50; // An explosion pool explosions = game.add.group(); explosions.createMultiple(30, 'kaboom'); explosions.forEach(setupInvader, this); // All this does is basically start the invaders moving. Notice we're move the Group they belong to, rather than the invaders directly. var tween = game.add.tween(aliens).to({x: 200}, 2000, Phaser.Easing.Linear.None, true, 0, 1000, true); // When the tween completes it calls descend, before looping again tween.onComplete.add(descend, this); // And some controls to play the game with cursors = game.input.keyboard.createCursorKeys(); fireButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); } function setupInvader (invader) { invader.anchor.x = 0.5; invader.anchor.y = 0.5; invader.animations.add('kaboom'); } function descend() { aliens.y += 10; } function update() { // Scroll the background starfield.tilePosition.y += 2; // Reset the player, then check for movement keys player.body.velocity.setTo(0, 0); if (cursors.left.isDown) { player.body.velocity.x = -200; } else if (cursors.right.isDown) { player.body.velocity.x = 200; } // Firing? if (fireButton.isDown) { fireBullet(); } // Run collision game.physics.collide(bullets, aliens, collisionHandler, null, this); } function fireBullet () { // To avoid them being allowed to fire too fast we set a time limit if (game.time.now > bulletTime) { // Grab the first bullet we can from the pool bullet = bullets.getFirstExists(false); if (bullet) { // And fire it bullet.reset(player.x, player.y + 8); bullet.body.velocity.y = -400; bulletTime = game.time.now + 200; } } } function resetBullet (bullet) { // Called if the bullet goes out of the screen bullet.kill(); } function collisionHandler (bullet, alien) { // When a bullet hits an alien we kill them both bullet.kill(); alien.kill(); // Increase the score // And create an explosion :) var explosion = explosions.getFirstDead(); explosion.reset(alien.body.x, alien.body.y); explosion.play('kaboom', 30, false, true); }