How to create a countdown timer in WoofJS

In this tutorial you will learn how to create a countdown timer in WoofJS like the one below.

countdowntimer

Go to woofjs.com and create a new project. Use the code below to create a countdown timer and customise it as you like. Pay attention to the comments in the code explainig how it works.

// Add a background
setBackdropURL("./docs/images/war-backdrop.png")
setBackdropStyle("cover")

// Create a variable for time to countdown from
var time = 30

// Create text variable to display time
var timertext = new Text({
  text: time, 
  size: 100
})

// Create text variable to display 'game over'
var gameovertext = new Text({
  text: "GAME OVER!",
  size: 30,
  y: 100
})

// Hide 'game over' at start of game
gameovertext.hide()

// Every 1 second...
every(1, "second", () => {
  // Check that time is not up
  if(time > 0){
    // Make time go down by one second
    time -= 1
  }
  // Update timer text to show new time
  timertext.text = time
  
  // Display 'game over' when time is up
  if(time === 0){
    gameovertext.show()
  }
})