|
21 | 21 | ending. |
22 | 22 | */ |
23 | 23 |
|
| 24 | +var keycodes = new Keycodes(); |
| 25 | + |
24 | 26 | // Creates an instance of the Game class. |
25 | 27 | function Game() { |
26 | 28 |
|
@@ -196,6 +198,35 @@ Game.prototype.keyDown = function(keyCode) { |
196 | 198 | } |
197 | 199 | }; |
198 | 200 |
|
| 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 | + |
199 | 230 | // Inform the game a key is up. |
200 | 231 | Game.prototype.keyUp = function(keyCode) { |
201 | 232 | delete this.pressedKeys[keyCode]; |
@@ -236,11 +267,11 @@ WelcomeState.prototype.draw = function(game, dt, ctx) { |
236 | 267 | ctx.fillText("Space Invaders", game.width / 2, game.height/2 - 40); |
237 | 268 | ctx.font="16px Arial"; |
238 | 269 |
|
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); |
240 | 271 | }; |
241 | 272 |
|
242 | 273 | WelcomeState.prototype.keyDown = function(game, keyCode) { |
243 | | - if(keyCode == 32) /*space*/ { |
| 274 | + if(keyCode == keycodes.space) /*space*/ { |
244 | 275 | // Space starts the game. |
245 | 276 | game.level = 1; |
246 | 277 | game.score = 0; |
@@ -274,7 +305,7 @@ GameOverState.prototype.draw = function(game, dt, ctx) { |
274 | 305 | }; |
275 | 306 |
|
276 | 307 | GameOverState.prototype.keyDown = function(game, keyCode) { |
277 | | - if(keyCode == 32) /*space*/ { |
| 308 | + if(keyCode == keycodes.space) /*space*/ { |
278 | 309 | // Space restarts the game. |
279 | 310 | game.lives = 3; |
280 | 311 | game.score = 0; |
@@ -344,17 +375,17 @@ PlayState.prototype.update = function(game, dt) { |
344 | 375 | // event for smooth movement, otherwise the ship would move |
345 | 376 | // more like a text editor caret. |
346 | 377 |
|
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]) { |
349 | 380 | this.ship.x -= this.shipSpeed * dt; |
350 | 381 | } |
351 | 382 |
|
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]){ |
354 | 385 | this.ship.x += this.shipSpeed * dt; |
355 | 386 | } |
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]) { |
358 | 389 | this.fireRocket(); |
359 | 390 | } |
360 | 391 |
|
@@ -579,11 +610,11 @@ PlayState.prototype.draw = function(game, dt, ctx) { |
579 | 610 |
|
580 | 611 | PlayState.prototype.keyDown = function(game, keyCode) { |
581 | 612 |
|
582 | | - if(keyCode == 32) { |
| 613 | + if(keyCode == keycodes.space) { |
583 | 614 | // Fire! |
584 | 615 | this.fireRocket(); |
585 | 616 | } |
586 | | - if(keyCode == 80) { |
| 617 | + if(keyCode == keycodes.p) { |
587 | 618 | // Push the pause state. |
588 | 619 | game.pushState(new PauseState()); |
589 | 620 | } |
@@ -613,7 +644,7 @@ function PauseState() { |
613 | 644 |
|
614 | 645 | PauseState.prototype.keyDown = function(game, keyCode) { |
615 | 646 |
|
616 | | - if(keyCode == 80) { |
| 647 | + if(keyCode == keycodes.p) { |
617 | 648 | // Pop the pause state. |
618 | 649 | game.popState(); |
619 | 650 | } |
|
0 commit comments