Game Development Reference
In-Depth Information
Now that you've calculated the number of minutes and seconds remaining, you can create a string
that you draw on the screen:
this.text = minutes + ":" + seconds;
if (seconds < 10)
this.text = minutes + ":0" + seconds;
You set the color of the text to yellow so it better fits the design of the game:
this.color = powerupjs.Color.yellow;
Finally, you want to warn the player if they don't have a lot of time left to finish the level. You do this
by alternating between red and yellow when printing the text on the screen. You can do this with an
if instruction and a clever use of the modulus operator:
if (this._timeLeft <= 10 && seconds % 2 === 0)
this.color = powerupjs.Color.red;
Even though calculating things with time in this manner is sufficient for the Tick Tick game, you
might find yourself wanting to do more complex calculations with time. JavaScript has the Date
object, which represents time and allows more advanced handling of time, including time zones,
conversion to a string, and so on.
Making the Timer Go Faster or Slower
Depending on the kind of tile the player is walking on, time should go faster or slower. Walking on
a hot tile increases the speed at which time passes, whereas walking on an ice tile decreases it. To
allow for a timer that runs at different speeds, you introduce a multiplier value in the TimerGameObject
class. This value is stored as a member variable, and you initially set the multiplier to 1:
this.multiplier = 1;
Taking this multiplier into account while the timer is running is fairly easy. You simply multiply the
passed time by the multiplier in the update method, and you're done:
this._timeLeft -= delta * this.multiplier;
Now that you can change the speed at which time passes, you can do this depending on the kind of
tile the player is walking on. In the Player class, you already maintain a variable walkingOnIce , which
indicates whether the player is walking on an ice tile. In order to handle hot tiles as well, you define
another variable walkingOnHot , in which you keep track of whether the player is walking on a hot tile.
To determine the value of this variable, you follow the same approach you did for the walkingOnIce
variable. In the handleCollisions method, you initially set this variable to false :
this.walkingOnHot = false;
 
Search WWH ::




Custom Search