Input Manager
The input manager was a necessity because input’s are handled within javascript as events, but my game ran in a big loop- and the code in my loop cannot simply respond to an event whenever it’s raised; input needs to be checked and acted upon within a specific part of that loop (at the start of the Update section). So I had to put together the input handler, which is a really simple class that simply handles the events of the keypresses, and sets flags to indicate which key is being pressed.
So the user could start hold down the left arrow button while my loop is in the draw phase- obviously the code won’t be able to respond normally- but now that key press is immidiatly acted upon by the input manager and a flag set to true to indicate the left arrow is being depressed. My code then eventually comes back round to the Update phase and can check the flags of the input manager to find out what’s happened since it’s last cycle.
Looking at the code will probably make more sense than me trying to explain it;
/*!
* Generic Game Input Manager Class
* http://8weekgame.shawson.co.uk/
*
* Copyright 2010, Shaw Young
* Released under the MIT, BSD and GPL Licenses.
* http://www.opensource.org/licenses/bsd-license.php
* http://www.opensource.org/licenses/mit-license.php
* http://www.opensource.org/licenses/gpl-2.0.php
*
* Date: Mon Jul 17 17:00:00 2010
*/
function ManicMinorKeyboardInputManager() {
this.Reset();
}
ManicMinorKeyboardInputManager.prototype.press = function (e) {
var KeyID = e.keyCode;
this.LastKeyPressed = KeyID;
this.LastActivity = new Date().getTime();
switch (KeyID) {
case 19: //Keyboard Pause Key
if (this.Pause)
this.Pause = false;
else
this.Pause = true;
break;
case 37: // Left Arrow
this.Left = true;
break;
case 38: // Up Arrow
this.Up = true;
this.Jump = true;
break;
case 39: // Right Arrow
this.Right = true;
break;
case 40: // Down Arrow
this.Down = true;
break;
case 32: // Space Bar
this.Jump = true;
case 13: // return
this.Start = true;
}
}
ManicMinorKeyboardInputManager.prototype.release = function (e) {
var KeyID = e.keyCode;
this.LastActivity = new Date().getTime();
switch (KeyID) {
/* - I want pause to be a toggle!
case 19: //Keyboard Pause Key
this.Pause = false;
break;
*/
case 37: // Left Arrow
this.Left = false;
break;
case 38: // Up Arrow
this.Up = false;
this.Jump = false;
break;
case 39: // Right Arrow
this.Right = false;
break;
case 40: // Down Arrow
this.Down = false;
break;
case 32: // Space Bar
this.Jump = false;
case 13: // return
this.Start = false;
}
}
ManicMinorKeyboardInputManager.prototype.Reset = function () {
this.Up = false;
this.Down = false;
this.Left = false;
this.Right = false;
this.Jump = false;
this.Pause = false;
this.Menu = false;
this.Start = false;
this.LastKeyPressed = 0;
this.LastActivity = new Date().getTime();
}
- No comments yet.










