Game Development Reference
In-Depth Information
Exploring and Interacting
This chapter explains how users can interact with our games. We'll also cover some
physics and use what we've learned to create a basic first-person shooter game.
Keyboard movement and mouse looking
In order to move our camera around, we're going to encapsulate some state, so let's
define a KeyboardControls class in a new JavaScript file:
function KeyboardControls(object, options) {
this.object = object;
options = options || {};
this.domElement = options.domElement || document;
this.moveSpeed = options.moveSpeed || 1;
this.domElement.addEventListener('keydown', this.onKeyDown.
bind(this), false);
this.domElement.addEventListener('keyup', this.onKeyUp.bind(this),
false);
}
KeyboardControls.prototype = {
update: function() {
if (this.moveForward) this.object.translateZ(-this.moveSpeed);
if (this.moveBackward) this.object.translateZ( this.moveSpeed);
if (this.moveLeft) this.object.translateX(-this.moveSpeed);
if (this.moveRight) this.object.translateX( this.moveSpeed);
},
onKeyDown: function (event) {
switch (event.keyCode) {
case 38: /*up*/
Search WWH ::




Custom Search