var config = { type: Phaser.WEBGL, width: 800, height: 600, backgroundColor: '#2d2d2d', parent: 'phaser-example', scene: { preload: preload, create: create, update: update } }; var bullets; var ship; var speed; var stats; var lastFired = 0; var isDown = false; var mouseX = 0; var mouseY = 0; var game = new Phaser.Game(config); function preload () { this.load.setBaseURL('https://cdn.phaserfiles.com/v355'); this.load.image('ship', 'assets/sprites/ship.png'); this.load.image('bullet1', 'assets/sprites/bullets/bullet11.png'); } function create () { var Bullet = new Phaser.Class({ Extends: Phaser.GameObjects.Image, initialize: function Bullet (scene) { Phaser.GameObjects.Image.call(this, scene, 0, 0, 'bullet1'); this.incX = 0; this.incY = 0; this.lifespan = 0; this.speed = Phaser.Math.GetSpeed(600, 1); }, fire: function (x, y) { this.setActive(true); this.setVisible(true); // Bullets fire from the middle of the screen to the given x/y this.setPosition(400, 300); var angle = Phaser.Math.Angle.Between(x, y, 400, 300); this.setRotation(angle); this.incX = Math.cos(angle); this.incY = Math.sin(angle); this.lifespan = 1000; }, update: function (time, delta) { this.lifespan -= delta; this.x -= this.incX * (this.speed * delta); this.y -= this.incY * (this.speed * delta); if (this.lifespan <= 0) { this.setActive(false); this.setVisible(false); } } }); bullets = this.add.group({ classType: Bullet, maxSize: 50, runChildUpdate: true }); ship = this.add.sprite(400, 300, 'ship').setDepth(1); this.input.on('pointerdown', function (pointer) { isDown = true; mouseX = pointer.x; mouseY = pointer.y; }); this.input.on('pointermove', function (pointer) { mouseX = pointer.x; mouseY = pointer.y; }); this.input.on('pointerup', function (pointer) { isDown = false; }); } function update (time, delta) { if (isDown && time > lastFired) { var bullet = bullets.get(); if (bullet) { bullet.fire(mouseX, mouseY); lastFired = time + 50; } } ship.setRotation(Phaser.Math.Angle.Between(mouseX, mouseY, ship.x, ship.y) - Math.PI / 2); }
Scan to open on your mobile device
var config = { type: Phaser.WEBGL, width: 800, height: 600, backgroundColor: '#2d2d2d', parent: 'phaser-example', scene: { preload: preload, create: create, update: update } }; var bullets; var ship; var speed; var stats; var lastFired = 0; var isDown = false; var mouseX = 0; var mouseY = 0; var game = new Phaser.Game(config); function preload () { this.load.setBaseURL('https://cdn.phaserfiles.com/v355'); this.load.image('ship', 'assets/sprites/ship.png'); this.load.image('bullet1', 'assets/sprites/bullets/bullet11.png'); } function create () { var Bullet = new Phaser.Class({ Extends: Phaser.GameObjects.Image, initialize: function Bullet (scene) { Phaser.GameObjects.Image.call(this, scene, 0, 0, 'bullet1'); this.incX = 0; this.incY = 0; this.lifespan = 0; this.speed = Phaser.Math.GetSpeed(600, 1); }, fire: function (x, y) { this.setActive(true); this.setVisible(true); // Bullets fire from the middle of the screen to the given x/y this.setPosition(400, 300); var angle = Phaser.Math.Angle.Between(x, y, 400, 300); this.setRotation(angle); this.incX = Math.cos(angle); this.incY = Math.sin(angle); this.lifespan = 1000; }, update: function (time, delta) { this.lifespan -= delta; this.x -= this.incX * (this.speed * delta); this.y -= this.incY * (this.speed * delta); if (this.lifespan <= 0) { this.setActive(false); this.setVisible(false); } } }); bullets = this.add.group({ classType: Bullet, maxSize: 50, runChildUpdate: true }); ship = this.add.sprite(400, 300, 'ship').setDepth(1); this.input.on('pointerdown', function (pointer) { isDown = true; mouseX = pointer.x; mouseY = pointer.y; }); this.input.on('pointermove', function (pointer) { mouseX = pointer.x; mouseY = pointer.y; }); this.input.on('pointerup', function (pointer) { isDown = false; }); } function update (time, delta) { if (isDown && time > lastFired) { var bullet = bullets.get(); if (bullet) { bullet.fire(mouseX, mouseY); lastFired = time + 50; } } ship.setRotation(Phaser.Math.Angle.Between(mouseX, mouseY, ship.x, ship.y) - Math.PI / 2); }