I’m looking for a way to make a countdown reset when a player does something.
Say the player needs to find a matching item in a set time. If they succeed, the timer resets and a new challenge is set. If they fail, the timer runs out and they lose a life.
Any examples of a resettable timer I can use or any other advice would be appreciated
The logic should be fairly straight forward. There’s a delta parameter passed to a scene’s update method, which is how long in miliseconds since last update. So you could just …
var player = this.add.sprite(..);
// the counter we use to track how long the player has left to find item
var countdown = 0;
// scene update loop
function update(time, delta) {
// only update the countdown if it's been set
if (countdown >= 0) {
// update the countdown
countdown -= delta * 0.001; // subtract time in seconds
// if the countdown is finished..
if (countdown <= 0) {
player.lose_a_life(); // < this also needs defining
}
}
}
All you have to do is remember to set the countdown timer to a positive value (in seconds) when the player picks up that first item. When the countdown runs out the condition is triggered and the player loses a life.