Skip to content

Commit 2e673ce

Browse files
authored
Merge pull request #7 from CLSB-Programming/develop-touch
v1.2
2 parents 4256118 + 72e7c0f commit 2e673ce

4 files changed

Lines changed: 86 additions & 14 deletions

File tree

index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
<canvas id="gameCanvas"></canvas>
1313
</div>
1414
<div id="info">
15-
<p>The invaders get faster and drop more bombs as you complete each level! | Controls: WASD / Arrow Keys + Space</p>
15+
<p>The invaders get faster and drop more bombs as you complete each level! | Controls: WASD / Arrow Keys + Space / Touch</p>
1616
<p><a id="muteLink" href="#" onclick="toggleMute()">Mute</a> |
1717
<a href="https://github.com/dwmkerr/spaceinvaders">Original Project</a> |
1818
<a href="https://github.com/CLSB-Programming/spaceinvaders">GitHub Project</a> |
1919
v1.1</p>
2020
</div>
2121

22+
<script src="js/keycodes.js"></script>
2223
<script src="js/starfield.js"></script>
2324
<script src="js/spaceinvaders.js"></script>
2425
<script src="js/setup.js"></script>

js/keycodes.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Keycodes.js
2+
// A list of all key codes needed for the game.
3+
4+
function Keycodes() {
5+
6+
this.space = 32;
7+
8+
this.w = 87;
9+
this.a = 65;
10+
this.d = 68;
11+
12+
this.p = 80;
13+
14+
this.arrows = {
15+
up: 38,
16+
left: 37,
17+
right: 39
18+
}
19+
20+
this.touch = {
21+
up: 38,
22+
left: 37,
23+
right: 39
24+
}
25+
}

js/setup.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ canvas.height = 600;
1111

1212
// Create the game.
1313
var game = new Game();
14+
var keycodes = new Keycodes();
1415

1516
// Initialise it with the game canvas.
1617
game.initialise(canvas);
@@ -22,7 +23,7 @@ game.start();
2223
window.addEventListener("keydown", function keydown(e) {
2324
var keycode = e.which || window.event.keycode;
2425
// Supress further processing of left/right/space/a/d/w/up (37/29/32/65/68/87/38)
25-
if(keycode == 37 || keycode == 39 || keycode == 32 || keycode == 65 || keycode == 68 || keycode == 87 || keycode == 38) {
26+
if(keycode == keycodes.arrows.left || keycode == keycodes.arrows.right || keycode == keycodes.space || keycode == 65 || keycode == 68 || keycode == 87 || keycode == 38) {
2627
e.preventDefault();
2728
}
2829
game.keyDown(keycode);
@@ -33,6 +34,20 @@ window.addEventListener("keyup", function keydown(e) {
3334
game.keyUp(keycode);
3435
});
3536

37+
// Touch Input
38+
39+
window.addEventListener("touchstart", function (e) {
40+
game.touchstart(e);
41+
}, false);
42+
43+
window.addEventListener('touchend', function(e){
44+
game.touchend(e);
45+
}, false);
46+
47+
window.addEventListener('touchmove', function(e){
48+
game.touchmove(e);
49+
}, false);
50+
3651
function toggleMute() {
3752
game.mute();
3853
document.getElementById("muteLink").innerText = game.sounds.mute ? "Unmute" : "Mute";

js/spaceinvaders.js

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
ending.
2222
*/
2323

24+
var keycodes = new Keycodes();
25+
2426
// Creates an instance of the Game class.
2527
function Game() {
2628

@@ -196,6 +198,35 @@ Game.prototype.keyDown = function(keyCode) {
196198
}
197199
};
198200

201+
// Touch
202+
Game.prototype.touchstart = function(s) {
203+
if(this.currentState() && this.currentState().keyDown) {
204+
this.currentState().keyDown(this, keycodes.space);
205+
}
206+
};
207+
208+
Game.prototype.touchend = function(s) {
209+
delete this.pressedKeys[keycodes.arrows.right];
210+
delete this.pressedKeys[keycodes.arrows.left];
211+
};
212+
213+
var previousX = 0;
214+
215+
Game.prototype.touchmove = function(e) {
216+
var currentX = e.changedTouches[0].pageX;
217+
if (previousX > 0){
218+
if (currentX > previousX){
219+
delete this.pressedKeys[keycodes.touch.left];
220+
this.pressedKeys[keycodes.touch.right] = true;
221+
}
222+
else{
223+
delete this.pressedKeys[keycodes.touch.right];
224+
this.pressedKeys[keycodes.touch.left] = true;
225+
}
226+
}
227+
previousX = currentX;
228+
};
229+
199230
// Inform the game a key is up.
200231
Game.prototype.keyUp = function(keyCode) {
201232
delete this.pressedKeys[keyCode];
@@ -236,11 +267,11 @@ WelcomeState.prototype.draw = function(game, dt, ctx) {
236267
ctx.fillText("Space Invaders", game.width / 2, game.height/2 - 40);
237268
ctx.font="16px Arial";
238269

239-
ctx.fillText("Press 'Space' to start.", game.width / 2, game.height/2);
270+
ctx.fillText("Press 'Space' or tap to start.", game.width / 2, game.height/2);
240271
};
241272

242273
WelcomeState.prototype.keyDown = function(game, keyCode) {
243-
if(keyCode == 32) /*space*/ {
274+
if(keyCode == keycodes.space) /*space*/ {
244275
// Space starts the game.
245276
game.level = 1;
246277
game.score = 0;
@@ -274,7 +305,7 @@ GameOverState.prototype.draw = function(game, dt, ctx) {
274305
};
275306

276307
GameOverState.prototype.keyDown = function(game, keyCode) {
277-
if(keyCode == 32) /*space*/ {
308+
if(keyCode == keycodes.space) /*space*/ {
278309
// Space restarts the game.
279310
game.lives = 3;
280311
game.score = 0;
@@ -344,17 +375,17 @@ PlayState.prototype.update = function(game, dt) {
344375
// event for smooth movement, otherwise the ship would move
345376
// more like a text editor caret.
346377

347-
// Left Movement Keys : A Key / Left Arrow Key
348-
if(game.pressedKeys[65] || game.pressedKeys[37]) {
378+
// Left Movement Keys : A Key / Left Arrow Key / Touch Left
379+
if(game.pressedKeys[keycodes.a] || game.pressedKeys[keycodes.arrows.left] || game.pressedKeys[keycodes.touch.left]) {
349380
this.ship.x -= this.shipSpeed * dt;
350381
}
351382

352-
// Right Movement Keys: D Key / Right Arrow Key
353-
if(game.pressedKeys[68] || game.pressedKeys[39]){
383+
// Right Movement Keys: D Key / Right Arrow Key / Touch Right
384+
if(game.pressedKeys[keycodes.d] || game.pressedKeys[keycodes.arrows.right] || game.pressedKeys[keycodes.touch.right]){
354385
this.ship.x += this.shipSpeed * dt;
355386
}
356-
// Fire Keys : Space Key / W Key / Up Key
357-
if(game.pressedKeys[32] || game.pressedKeys[87] || game.pressedKeys[38]) {
387+
// Fire Keys : Space Key / W Key / Up Key / Touch Up
388+
if(game.pressedKeys[keycodes.space] || game.pressedKeys[keycodes.w] || game.pressedKeys[keycodes.arrows.up] || game.pressedKeys[keycodes.touch.up]) {
358389
this.fireRocket();
359390
}
360391

@@ -579,11 +610,11 @@ PlayState.prototype.draw = function(game, dt, ctx) {
579610

580611
PlayState.prototype.keyDown = function(game, keyCode) {
581612

582-
if(keyCode == 32) {
613+
if(keyCode == keycodes.space) {
583614
// Fire!
584615
this.fireRocket();
585616
}
586-
if(keyCode == 80) {
617+
if(keyCode == keycodes.p) {
587618
// Push the pause state.
588619
game.pushState(new PauseState());
589620
}
@@ -613,7 +644,7 @@ function PauseState() {
613644

614645
PauseState.prototype.keyDown = function(game, keyCode) {
615646

616-
if(keyCode == 80) {
647+
if(keyCode == keycodes.p) {
617648
// Pop the pause state.
618649
game.popState();
619650
}

0 commit comments

Comments
 (0)