Game Development Reference
In-Depth Information
case 87: /*W*/ this.moveForward = true; break;
case 37: /*left*/
case 65: /*A*/ this.moveLeft = true; break;
case 40: /*down*/
case 83: /*S*/ this.moveBackward = true; break;
case 39: /*right*/
case 68: /*D*/ this.moveRight = true; break;
}
},
onKeyUp: function (event) {
switch(event.keyCode) {
case 38: /*up*/
case 87: /*W*/ this.moveForward = false; break;
case 37: /*left*/
case 65: /*A*/ this.moveLeft = false; break;
case 40: /*down*/
case 83: /*S*/ this.moveBackward = false; break;
case 39: /*right*/
case 68: /*D*/ this.moveRight = false; break;
}
}
};
In the constructor, we added listeners for the keydown event and the keyup event
so that when a key is pressed, we can keep track of the direction in which we
should move. (In JavaScript, pressed keys are identified by numeric key codes.) In
our update method, we just move in the specified direction. This is accomplished
by checking flags that we set during key events so that we can poll the keyboard
state during each frame. We can then use the controller by declaring it with new
KeyboardControls(camera) and make it affect the camera in every frame by calling
controls.update(delta) in our animation loop.
 
Search WWH ::




Custom Search