3D Game Programming

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

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
});
</script>

A1.5 Code: Functions: Use and Use Again


We intentionally broke a lot of things as we explored functions in Chapter 5,
Functions: Use and Use Again, on page 49. A copy of the code that works
follows (note that it doesn’t include the recursion example).

<body></body>
<script src="http://gamingJS.com/Three.js"></script>
<script src="http://gamingJS.com/ChromeFixes.js"></script>
<script>
varlog = makeLog();
logMessage(hello("President Obama"), log);
logMessage(hello("Mom"), log);
logMessage(hello("Purple Fruit Monster"), log);
logMessage(hello("Chris"), log);

/*
// Missing a curly brace - this won't work!
function hello(name) {
return 'Hello, ' + name + '! You look very pretty today :)';

*/

functionhello(name) {
return'Hello, '+ name +'! You look very pretty today :)';
}

functionmakeLog() {
varholder = document.createElement('div');
holder.style.height ='75px';
holder.style.width ='450px';

Appendix 1. Project Code • 222


Prepared exclusively for Michael Powell report erratum • discuss

Free download pdf