You can keep track of the elapsed time (or remaining time) in your game. You can also add timers to make certain events occur after a designated amount of time has elapsed.
Event timers work by calling a custom function (that you create) once the designated amount of time has elapsed. Your custom function contains whatever actions you want to perform for the event.
For example, your game might have power-ups that give the player a boost in speed for a certain amount of time. Once the player collects a power-up, your game could start a timer so the speed boost will be stopped by a custom function after 10 seconds.
Useful Constants for Setting Timers
Add Timer for Single Event
Add Timer for Repeated Event
Add Timer for Looping Event
Get Elapsed Game Time
Calculate Elapsed Time Between Events
Create Count Up Timer (Elapsed Game Time)
Create Countdown Timer (Game Time Remaining)
Start Timer After Specific Key Pressed
Timers are set in milliseconds (1000 ms = 1 second). You can list a specific number (e.g., 5000
would be 5 seconds) or a calculated number (e.g., 1000 * 5
would also be 5 seconds).
Phaser has two constants that are helpful when setting a timer:
Phaser.Timer.SECOND
represents the number of milliseconds in one second (1000 ms = 1 second).
Phaser.Timer.MINUTE
represents the number of milliseconds in one minute (60000 ms = 1 minute).
For example, imagine that you need to set a timer for 2 minutes. Any of these could be used to represent the number of milliseconds in 2 minutes:
120000
1000 * 60 * 2
Phaser.Timer.SECOND * 120
Phaser.Timer.MINUTE * 2
Even though any of those could be used, the last option is probably the easiest to understand when reading the code (and the least likely for you to make a mistake when typing the code).
You can set a timer for a single event. When the designated amount of time has elapsed, a custom function will be called to perform whatever actions you decide.
The generic code for setting a single event timer is:
time
is the amount of time (in milliseconds) that should elapse before calling the event function. Change this to any time that you need.
timerEvent
is the name of the custom function to run when the timer is completed. List the function name without parentheses. You will have to create this custom function in your game. Change this to any unique function name that makes sense for your game.
If you want the timer to start when the game starts, then set the timer in your create()
function.
If you want to start the timer at some other point during the game, then set the timer in your update()
function or in a custom function.
For example, to set a 10 second timer:
Then be sure to add your custom function after the update()
function:
You can set a timer for a repeated event. When the designated amount of time has elapsed, a custom function will be called to perform whatever actions you decide. Then a new timer will be started — this will repeat however many times you specify.
The generic code for setting a repeated event timer is:
time
represents the amount of time (in milliseconds) that should elapse before calling the event function. Change this to any time that you need.
quantity
represents the number of times that the timer should repeat itself.
timerEvent
is the name of the custom function to run when the timer is completed. List the function name without parentheses. You will have to create this custom function in your game. Change this to any unique function name that makes sense for your game.
If you want the timer to start when the game starts, then set the timer in your create()
function.
If you want to start the timer at some other point during the game, then set the timer in your update()
function or in a custom function.
For example, to set a 10 second timer that will repeat itself 5 times:
Then be sure to add your custom function after the update()
function:
You can set a timer for a looping event. When the designated amount of time has elapsed, a custom function will be called to perform whatever actions you decide. Then a new timer will be started — this will keep repeating in an endless loop throughout the game.
The generic code for setting a looping event timer is:
time
represents the amount of time (in milliseconds) that should elapse before calling the event function. Change this to any time that you need.
timerEvent
is the name of the custom function to run when the timer is completed. List the function name without parentheses. You will have to create this custom function in your game. Change this to any unique function name that makes sense for your game.
If you want the timer to start when the game starts, then set the timer in your create()
function.
If you want to start the timer at some other point during the game, then set the timer in your update()
function or in a custom function.
For example, to set a 30 second timer that will keep repeating in a loop:
Then be sure to add your custom function after the update()
function:
Phaser keeps track of how much total time has elapsed (in seconds) since the game started. You can get this value and assign it to a variable to do something with the result.
The total elapsed time will be a decimal number representing exactly how many seconds have elapsed since the game started.
If you want to get the exact time that a particular event occurred, you would use this:
If you want to get the elasped time rounded down to the nearest whole second, you would use this:
You can use global variables to store the exact time that two different events occurred in the game, and then subtract the times to calculate how much time passed between them.
For example, get the time that the first event occurred and store it in a global variable:
Then get the time that the second event occurred and store it in another variable. Subtract the two times to get the amount of time that elapsed between the two events:
As a more specific example, imagine that your game rewards the player for how fast they progress from one checkpoint to the next. Your game could have an overlap()
method to detect when the player reaches a checkpoint (assume that each checkpoint is a member of a group of sprites). When an overlap occurs, a custom function is called that gets the exact time the event occurred and awards a score point bonus based on how much time elapsed since the previous checkpoint.
For example, some of the code might look like this:
You can create a "count up" timer to display the elapsed time since the game started.
Create a global variable named timeText
that will be used to display the count up timer.
In your create()
function, add a text object by assigning it to timeText
. If you have an extended game world, be sure to make the text stay fixed to the camera view.
Next you'll add a custom function that get the current elapsed time (rounded down the nearest second) and then convert that into minutes and seconds to display on-screen as text.
After your update()
function, add this custom function:
update()
Now you just need to call the custom function in your update()
function, so it will update the count up timer every game loop.
Just include this as the first line of code inside your update()
function:
You can create a countdown timer to display the amount of time remaining in the game (based on a specified time limit). The timer starts counting down when the game starts.
You could display the countdown timer as text or as a timer bar. Both options will be provided below.
Before your preload()
function, declare a global variable named timeLimit
for your countdown timer, and assign it a value representing the number of seconds for the countdown timer (e.g., 120
would represent a 2 minute countdown timer). Declare a global variable named timeOver
that will be used to detect when the countdown timer has reached zero. Also declare a global variable named timeText
that will be used to display the time remaining.
In your create()
function, add a text object by assigning it to timeText
. If you have an extended game world, be sure to make the text stay fixed to the camera view.
Next you'll add a custom function that get the current elapsed time (rounded down the nearest second) and then convert that into minutes and seconds to display on-screen as text.
After your update()
function, add this custom function:
update()
Now you just need to call the custom function in your update()
function, so it will update the count up timer every game loop until the time runs out.
Just include this at the beginning of your update()
function:
Create an image of a solid rectangle (whatever color you want) that will be used as the timer bar (to represent time remaining). This example will use a green rectangle that is 200px in width and 20px in height:
Create an image of an identical-sized solid rectangle (of a different color) that will be used as a background bar (to represent elapsed time). This example will use a red rectangle that also is 200px in width and 20px in height:
Before your preload()
function, declare a global variable named timeLimit
for your countdown timer, and assign it a value representing the number of seconds for the countdown timer (e.g., 120
would represent a 2 minute countdown timer). Declare a global variable named timeOver
that will be used to detect when the countdown timer has reached zero. Finally, declare a global variable named timeBar
for the timer bar:
In your preload()
function, load the two rectangle images:
The background bar (red) and the timer bar (green) will be added to the game at the same position. However, the background bar will be added first, so it will be stacked behind the timer bar.
In your create()
function, add the background bar by assigning it to a local variable. Then add the timer bar at the same position by assigning it to its global variable.
You should also add a text label to the left of the bars by assigning it to a local variable.
If your game world is larger than your game display, then be sure to make the images and text label fixed to the camera.
As the amount of time remaining decreases, the width of the timer bar is supposed to shrink proportionately. When the timer bar shrinks, it will reveal some of the background bar behind it.
We can make the timer bar shrink by changing its scale
property. The correct value for the scale can be calculated by dividing the time remaining by the time limit.
After your update()
function, add this custom function:
update()
Now you just need to call the custom function in your update()
function, so it will update the count up timer every game loop until the time runs out.
Just include this at the beginning of your update()
function:
You can also make your timer wait to start until a specific key is pressed. The player won't be able to perform any other actions until the "start" key has been pressed.
In this example, we'll use the spacebar as the "start" key. Once the game is "started," this same key can be used instead for other normal game actions. For example, some games use the spacebar for actions such as jumping or firing a weapon.
This example will work with a countdown timer or a countup timer - but you'll need to use a slightly modified function for your timer that adjusts for the actual "start time" when the player first pressed the spacebar key.
Before your preload()
function, declare a global variable named started
, and assign it a value of false
(later when the spacebar key is first pressed, you'll change this variable to a value of true
).
Declare another global variable named startTime
, and assign it an initial value of zero (later this variable will be used to store the time when the spacebar key is first pressed).
Finally, declare a global variable named spacebar
that will be used to represent the spacebar key.
Within your create()
function, make the spacebar
variable represent user input using the spacebar key.
Again, if you also want to use the spacebar for other player actions (such as jumping, firing weapon, etc.) after the game is "started," you'll be able to do so.
Within your update()
function, you'll add some if
statements that will check whether the variable named started
has a value of true
or false
. These will be added after your game.physics.arcade.collide()
functions (which are usually listed at the beginning of the update()
function).
If started
has a value of true
, then the game will updated the time left in the countdown timer and check for any player input to perform normal game actions (such as moving, jumping, firing weapon, etc.).
However, if started
has a value of false
, then the game will only check to see if the spacebar was pressed. The game will NOT update the countdown timer (meaning it will act like it is "paused"), and the game won't allow the player to perform any other actions. The game is simply waiting for the player to press the spacebar.
Once the spacebar is pressed, the game will change the value of started
to true
, and store the current game time in the startTime
variable (so the timer can adjust itself based on the start time).
Finally, you'll have to modify your timer function to adjust for the start time (when the player first pressed the spacebar).
The custom function for your timer should be placed after the update()
function.
Here's a modified countdown timer function for displaying a shrinking time bar:
If you are using a countup timer, your modified timer function would instead look like this: