-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource.js
122 lines (87 loc) · 2.82 KB
/
source.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
var game = new Phaser.Game(318, 238, Phaser.CANVAS, '', { preload: preload, create: create, update: update });
function preload() {
game.load.image('phaser', 'assets/shmup-ship.png');
game.load.image('bullet', 'assets/bullet0.png');
game.load.audio('sfxLazer', ['assets/lazer.wav']);
}
var sprite;
var bullet;
var bullets;
var bulletTime = 0;
var sfxLazer;
// Left, right and space key for controls
var upKey;
var downKey;
var leftKey;
var rightKey;
var fireKey;
var escKey;
function create() {
game.physics.startSystem(Phaser.Physics.ARCADE);
game.stage.backgroundColor = '#2d2d2d';
bullets = game.add.group();
bullets.enableBody = true;
bullets.physicsBodyType = Phaser.Physics.ARCADE;
bullets.createMultiple(10, 'bullet');
bullets.callAll('events.onOutOfBounds.add', 'events.onOutOfBounds', resetBullet, this);
bullets.setAll('checkWorldBounds', true);
sprite = game.add.sprite(150, 180, 'phaser');
sfxLazer = game.add.audio('sfxLazer');
game.physics.enable(sprite, Phaser.Physics.ARCADE);
// Register the keys.
this.upKey = game.input.keyboard.addKey(Phaser.Keyboard.UP);
this.downKey = game.input.keyboard.addKey(Phaser.Keyboard.DOWN);
this.leftKey = game.input.keyboard.addKey(Phaser.Keyboard.LEFT);
this.rightKey = game.input.keyboard.addKey(Phaser.Keyboard.RIGHT);
this.fireKey = game.input.keyboard.addKey(Phaser.Keyboard.J);
this.escKey = game.input.keyboard.addKey(Phaser.Keyboard.ESC);
// Stop the following keys from propagating up to the browser
game.input.keyboard.addKeyCapture([ Phaser.Keyboard.UP, Phaser.Keyboard.DOWN, Phaser.Keyboard.LEFT, Phaser.Keyboard.RIGHT, Phaser.Keyboard.J, Phaser.Keyboard.ESC ]);
}
function update() {
sprite.body.velocity.x = 0;
sprite.body.velocity.y = 0;
// If true, it means that this key is down. If not, it means that the key is not down (was released/not pressed)
if (this.upKey.isDown)
{
sprite.body.velocity.y = -200;
}
if (this.downKey.isDown)
{
sprite.body.velocity.y = 200;
}
if (this.leftKey.isDown)
{
sprite.body.velocity.x = -200;
}
if (this.rightKey.isDown)
{
sprite.body.velocity.x = 200;
}
if (this.fireKey.isDown)
{
fireBullet();
}
if (this.escKey.isDown)
{
// nwjs related code to close the application
nw.App.closeAllWindows();
}
}
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;
sfxLazer.play();
}
}
}
// Called if the bullet goes out of the screen
function resetBullet (bullet) {
bullet.kill();
}