3D Game Programming

(C. Jardin) #1
functionanimate() {
requestAnimationFrame(animate);
scene.simulate();// run physics
renderer.render(scene, camera);
}

functiongameStep() {
if(ball.position.y < -100) resetBall(ball);
setTimeout(gameStep, 1000 / 60);
}

functionflashGoalLight(light, remaining) {
if(typeof(remaining) =='undefined') remaining = 9;

if(light.material.opacity == 0.4) {
light.material.ambient.setRGB(1,1,1);
light.material.emissive.setRGB(1,1,1);
light.material.color.setRGB(1,1,1);
light.material.opacity = 0.15;
}
else{
light.material.ambient.setRGB(1,0,0);
light.material.emissive.setRGB(1,0,0);
light.material.color.setRGB(1,0,0);
light.material.opacity = 0.4;
}

if(remaining > 0) {
setTimeout(function() {flashGoalLight(light, remaining-1);}, 500);
}
}
</script>

A1.17Code: Learning about JavaScript Objects


The code from Chapter 17, Project: Learning about JavaScript Objects, on page
159 , should look something like the following.

<body></body>
<script src="http://gamingJS.com/Three.js"></script>
<script src="http://gamingJS.com/ChromeFixes.js"></script>
<script>
// This is where stuff in our game will happen:
varscene =newTHREE.Scene();

// This is what sees the stuff:
varaspect_ratio = window.innerWidth / window.innerHeight;
varcamera =newTHREE.PerspectiveCamera(75, aspect_ratio, 1, 10000);
camera.position.z = 500;
scene.add(camera);

report erratum • discuss

Code: Learning about JavaScript Objects • 253


Prepared exclusively for Michael Powell

Free download pdf