Phaser: Understanding the Game Loop
This blog post is part of a series on Phaser. Check out the first post here.
In Phaser, we have three different scenes: preload()
, create()
, and update()
. While preload()
and create()
are run only once, update()
is constantly called in an never-ending loop until the game ends.
In this example, we create a text object that slowly moves towards the bottom right corner of the canvas:
1 | let text; |
Note that I added let text
at the top so we can reference it within both create()
and update()
functions.
In the update()
function, I modified the values of x
and y
properties. You can also modify other properties, such as angle
to rotate an object:
1 | function update() { |
Additionally, you can set the initial velocity of an object using the setVelocity()
function:
1 | text.setVelocity(20, 20); |
Alternatively, you can use setVelocityX()
and setVelocityY()
to set velocity on only one axis.