Java Reference
In-Depth Information
// decrease time by 1
time--;
// update the time displayed
update($timer,time);
// the game is over if the timer has reached 0
if(time <= 0) {
gameOver();
}
This function decreases the time variable that we initialized earlier by 1 , and then calls
the update() so that the time the player has left is updated and shown in the header. Last
of all, we check to see if the time has reached zero and, if it has, we call the gameOver()
function.
Finally, we have to add a line to the gameOver() function that will remove the interval
when the game has finished, otherwise it will continue to keep counting down past zero!
To stop this from happening, we place the following line of code anywhere inside the
gameOver() function:
// stop the countdown interval
window.clearInterval(interval);
Try playing the game now by opening index.htm in a browser and see how you go with the
added pressure of beating the clock. It should look similar to Figure 9.2 .
Search WWH ::




Custom Search