varpaused;
startGame(raft, river, scoreboard);
Other functions will use that variable to decide if they need to animate or
update the game. JavaScript is pretty uptight about when variables are
declared. The rule of thumb is that variables need to be declared before they’re
used. The paused variable will be used when startGame(), animate(), and gameStep()
are called, so we declare it before any of them are called.
We set paused for the first time in the startGame() function. Whenever the game
is started, which is what startGame() does, the game shouldn’t be paused. So
we set paused to false at the bottom of startGame():
functionstartGame(raft, river, scoreboard) {
varstart = river.river_points[100];
raft.__dirtyPosition = true;
raft.position.set(start.y, start.z + 100, 0);
raft.setLinearVelocity(newTHREE.Vector3());
scoreboard.resetTimer();
scoreboard.score(0);
scoreboard.message('');
updateCamera();
camera.lookAt(newTHREE.Vector3(start.y, 0, 0));
paused = false;
}
Next we tell the animate() function that it doesn’t have to render the scene when
the game is paused. That is, if paused is set to true, then we exit the animate()
function before updating the camera or rendering the scene:
functionanimate() {
requestAnimationFrame(animate);
if(paused)return;
updateCamera();
renderer.render(scene, camera);
}
We check for pausedafter calling requestAnimationFrame() so the animation function
will continue to work—even though it’s not doing anything. This way when
the game is reset and paused is set to true, the animation is still running and
the computer can update the camera without any extra work.
We do something similar in the gameStep() function. If the game is paused,
then we exit immediately from the function without completing any of the
usual steps:
report erratum • discuss
Setting the Finish Line • 199
Prepared exclusively for Michael Powell