3D Game Programming

(C. Jardin) #1
document.addEventListener('keydown',function(event) {
varcode = event.keyCode;
if(code == 37) marker.position.x = marker.position.x-5;// left
if(code == 38) marker.position.z = marker.position.z-5;// up
if(code == 39) marker.position.x = marker.position.x+5;// right
if(code == 40) marker.position.z = marker.position.z+5;// down

if(code == 67) is_cartwheeling = !is_cartwheeling; // C
if(code == 70) is_flipping = !is_flipping; // F
});

With that, we can move the avatar’s position with the keyboard, but when we
flip or cartwheel, the camera stays upright.

5.5 The Code So Far


If you would like to double-check your code so far, compare it to the code in
Section A1.4, Code: Moving Avatars, on page 220.

4.8 What’s Next


We covered a very important skill in this chapter. We’ll group objects like this
over and over as our gaming skills improve. Grouping simplifies moving things
together, as well as twisting, turning, growing, and shrinking things together.

Before adding even more stuff to our avatar, let’s take a break so that we can
explore JavaScript functions a bit more. We’re already using them to make
a forest, to animate, and to listen for events. There’s even more cool stuff that
we can do with them.

report erratum • discuss

6.5 The Code So Far


Prepared exclusively for Michael Powell

Free download pdf