Permalink
Browse files

Corrected key handling

  • Loading branch information...
Stuart Campbell Stuart Campbell
Stuart Campbell authored and Stuart Campbell committed Jun 4, 2011
1 parent 3737a5c commit ff3aea22893b8c50483ae05f2b61a988d577e723
Showing with 40 additions and 13 deletions.
  1. +21 −10 public/js/game/client.js
  2. +19 −3 public/js/game/kaboom.player.js
View
@@ -20,54 +20,60 @@ KaboomClient.prototype = {
onKeyDown : function(event){
- console.log('keydown');
var player = window.player;
var key = $.hotkeys.specialKeys[event.which] || String.fromCharCode(event.which).toLowerCase();
var playerChanged = false;
+ var handled = false;
if (key == 'left') {
+ handled = true;
playerChanged = player.goLeft();
}
if (key == 'right') {
+ handled = true;
playerChanged = player.goRight();
}
if (key == 'up') {
+ handled = true;
playerChanged = player.goUp();
}
if (key == 'down') {
+ handled = true;
playerChanged = player.goDown();
}
- if (playerChanged)
+ if (playerChanged){
this.notifyPlayerChanged();
- return false;
+ }
+ return !handled;
},
onKeyUp : function(event){
- console.log('keyup');
var player = window.player;
var key = $.hotkeys.specialKeys[event.which] || String.fromCharCode(event.which).toLowerCase();
-
+ var handled = false;
+
switch (key){
case 'left':
case 'right':
+ handled = true;
player.horizontalStop();
break
case 'up':
case 'down':
+ handled = true;
player.verticalStop();
break;
}
this.notifyPlayerChanged();
- return false;
-
+ return !handled;
},
@@ -109,10 +115,15 @@ KaboomClient.prototype = {
window.game.update();
renderer.update();
}, 1000/this.fps);
-
-
-
+ },
+
+ playerJoined: function(playerState){
+ var newPlayer = new KaboomPlayer();
+ newPlayer.copyStateFrom(playerState);
+ window.game.addPlayer(newPlayer);
}
+
+
};
@@ -26,19 +26,35 @@ function Velocity(dx, dy) {
KaboomPlayer.prototype = {
goLeft: function(){
- this.velocity.dx = -1;
+ if(this.velocity.dx != -1){
+ this.velocity.dx = -1;
+ return true;
+ }
+ return false;
},
goRight: function(){
- this.velocity.dx = 1;
+ if (this.velocity.dx != 1) {
+ this.velocity.dx = 1;
+ return true;
+ }
+ return false;
},
goUp: function(){
- this.velocity.dy = -1;
+ if(this.velocity.dy != -1){
+ this.velocity.dy = -1;
+ return true;
+ }
+ return false;
},
goDown: function(){
+ if(this.velocity.dy != 1){
this.velocity.dy = 1;
+ return true;
+ }
+ return false;
},
horizontalStop: function(){

0 comments on commit ff3aea2

Please sign in to comment.