Before making the game behave the way the help text says it should, we need
to teach the game what to do when time runs out. To do so, add the following
on the line after all of the scoreboard code:
vargame_over = false;
scoreboard.onTimeExpired(function() {
scoreboard.message("Game Over!");
game_over = true;
});
This tells the scoreboard that when time expires, it should run the function
that sets the scoreboard message to Game Over! and that the game_over variable
should be set to true.
That’s all there is to build the scoreboard. Now, let’s figure out a way for the
player to add points to the scoreboard.
11.3 Giving Trees a Little Wiggle
The goal of this game will be to find fruit, which we’ll call treasure, in the
trees. At any given time, only one tree will have treasure. To show which tree
it is, we’ll give it a little shake. But first we need a list of trees.
Find the code that created the forest—there should be four lines that make
different makeTree() calls. We added this way back in Chapter 4, Project: Moving
Avatars, on page 35. We need to make a little change to the part of the code
that adds the trees to the scene.
We start by adding two variables. The first is tree_with_treasure, which you might
have guessed will point to the tree that currently has treasure. The second
variable is a list containing all the trees, which we’ll call trees. We then push
into this list all the trees that will make up our forest. Change your four
makeTreeAt() lines to the following:
vartree_with_treasure;
vartrees = [];
trees.push(makeTreeAt( 500, 0));
trees.push(makeTreeAt(-500, 0));
trees.push(makeTreeAt( 750, -1000));
trees.push(makeTreeAt(-750, -1000));
To make this bit work, we need to change the makeTreeAt() function so that it
returns something. If makeTreeAt() doesn’t return anything, then we would be
report erratum • discuss
Giving Trees a Little Wiggle • 101
Prepared exclusively for Michael Powell