/
asteroids.js
87 lines (60 loc) · 1.89 KB
/
asteroids.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('bullet', 'assets/sprites/shmup-bullet.png');
game.load.image('ship', 'assets/sprites/thrust_ship.png');
}
var sprite;
var weapon;
var cursors;
var fireButton;
function create() {
// Creates 30 bullets, using the 'bullet' graphic
weapon = game.add.weapon(30, 'bullet');
// The bullet will be automatically killed when it leaves the world bounds
weapon.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS;
// The speed at which the bullet is fired
weapon.bulletSpeed = 600;
// Speed-up the rate of fire, allowing them to shoot 1 bullet every 60ms
weapon.fireRate = 100;
sprite = this.add.sprite(400, 300, 'ship');
sprite.anchor.set(0.5);
game.physics.arcade.enable(sprite);
sprite.body.drag.set(70);
sprite.body.maxVelocity.set(200);
// Tell the Weapon to track the 'player' Sprite
// With no offsets from the position
// But the 'true' argument tells the weapon to track sprite rotation
weapon.trackSprite(sprite, 0, 0, true);
cursors = this.input.keyboard.createCursorKeys();
fireButton = this.input.keyboard.addKey(Phaser.KeyCode.SPACEBAR);
}
function update() {
if (cursors.up.isDown)
{
game.physics.arcade.accelerationFromRotation(sprite.rotation, 300, 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 (fireButton.isDown)
{
weapon.fire();
}
game.world.wrap(sprite, 16);
}
function render() {
weapon.debug();
}