functionremoveItems() {
game_items.forEach(function(item) {
scene.remove(item);
});
game_items = [];
}
Adding items to the river is where things start to get interesting. The ground
still has the river_points property. We’ll use that list of points to randomly place
power-up fruit at a couple of places along the river. Randomly placing the fruit
will make each new game a different challenge for players.
Only we don’t want to be quite random about it. If it were completely random,
we might end up with two pieces of fruit in the same place or one right at the
start.
Recall that there are 100 faces in the ground and 100 points in the ground
that we are using to describe the middle of the river. Lets randomly place a
power-up fruit around river point 20 and another around point 70. The fol-
lowing will do what we need:
functionaddItems(ground, scoreboard) {
varpoints = ground.river_points;
varrandom20 = Math.floor(20 + 10*Math.random()),
fruit20 = addFruitPowerUp(points[random20], ground, scoreboard);
game_items.push(fruit20);
varrandom70 = Math.floor(70 + 10*Math.random()),
fruit70 = addFruitPowerUp(points[random70], ground, scoreboard);
game_items.push(fruit70);
}
The main purpose of this code block is to call the addFruitPowerUp() function that
we’ll build shortly. The fruit20 and fruit70 items are then pushed onto the list
of all game items (so that they can later be removed as needed).
The random20 and random70 numbers might look a little complicated at first,
but if you look closely, they ought to make some sense. Let’s look at just ran-
dom20 to better understand. The Math.random() function generates a number
between 0 and 1.
- If Math.random() is 0 , then 10Math.random() is 0 , making 20 + 10Math.random()
end up as 20. - If Math.random() is 0.5, then 10Math.random() is 5 , making 20 + 10Math.random()
end up as 25.
report erratum • discuss
Setting the Finish Line • 203
Prepared exclusively for Michael Powell