3D Game Programming

(C. Jardin) #1
if(is_flipping) {
avatar.rotation.x = avatar.rotation.x + 0.05;
}
renderer.render(scene, camera);
}
animate();

Here is the complete keyboard event listener for moving, flipping, and
cartwheeling our avatar.

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

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

If you’ve got it right, you should be able to make the avatar do flips and
cartwheels as it moves off the screen.

Actually, it’s pretty crazy that the avatar can leave the screen. We’ll fix that
in a bit, but first let’s add some trees for our avatar to walk through.

4.5 Building a Forest with Functions


We’ll need a lot of trees for our forest. We could build them one at a time, but
we’re not going to do that. Instead, let’s add the following JavaScript after all
of the avatar body parts:

makeTreeAt( 500, 0);
makeTreeAt(-500, 0);
makeTreeAt( 750, -1000);
makeTreeAt(-750, -1000);

Chapter 4. Project: Moving Avatars • 40


Prepared exclusively for Michael Powell report erratum • discuss

Free download pdf