Initialize the framework
This is the first of 16 tutorials to learn how to use Gamedev Phaser. Before we can start writing the game's functionality, we need to create a basic structure to render the game inside. This can be done using HTML—the Phaser framework will generate the required <canvas>
element.
The game's HTML
The HTML document structure is quite simple, as the game will be rendered entirely on the <canvas>
element generated by the framework. Using your favorite text editor, create a new HTML document, save it as index.html
, in a sensible location, and add the following code to it:
<!doctype html>
<html lang="en-US">
<head>
<meta charset="utf-8" />
<title>Gamedev Phaser Workshop - lesson 01: Initialize the framework</title>
<style>
* {
padding: 0;
margin: 0;
}
</style>
<script src="js/phaser.min.js"></script>
<script src="js/script.js" defer></script>
</head>
<body></body>
</html>
And create a new js
directory in the same location as your index.html
file, and create a new file called script.js
inside it. This is where we will write the JavaScript code that controls the game. Initially it should contain the following:
class ExampleScene extends Phaser.Scene {
preload() {}
create() {}
update() {}
}
const config = {
type: Phaser.CANVAS,
width: 480,
height: 320,
scene: ExampleScene,
};
const game = new Phaser.Game(config);
Downloading the Phaser code
Next, we need to go through the process of downloading the Phaser source code and applying it to our HTML document. This tutorial uses Phaser v3 (v3.90.0 as of the time of writing, though newer minor versions should work the same).
- Go to the Phaser download page.
- Choose an option that suits you best—we recommend the phaser.min.js option as it keeps the source code smaller, and you are unlikely to go through the source code anyway.
- Save the Phaser code in the
js
directory. If you use another file name, make sure to update thesrc
value of the first<script>
element in the HTML accordingly.
Walking through what we have so far
At this point, we have a charset
defined, <title>
, and some basic CSS in the header to reset the default margin
and padding
. We also have a <script>
element to apply the Phaser source code to the page. The body contains a second <script>
element, where we will write the JavaScript code to render the game and control it.
The <canvas>
element is generated automatically by the framework. We are initializing it by creating a new Phaser.Game
object and assigning it to the game
variable. The parameters are:
- The rendering method. The available options are
AUTO
,CANVAS
,WEBGL
,HEADLESS
. We can set eitherCANVAS
orWEBGL
explicitly or useAUTO
to let Phaser decide which one to use. It usually uses WebGL if available in the browser, falling back to Canvas 2D if not. The last option,HEADLESS
, is used for server-side rendering or testing, which is not relevant for this tutorial. - The width and height to set the
<canvas>
to. - The scene to add to the game. In this case, we are creating a new class called
ExampleScene
that extendsPhaser.Scene
. This class implements the methods that Phaser calls at different stages of the game lifecycle. We'll fill in these methods among them later:preload
takes care of preloading the assetscreate
is executed once when everything is loaded and readyupdate
is executed on every frame.
Running the application
To run the app, you cannot directly open the index.html
file, because later we will be loading external assets, which will be blocked by the browser's same-origin policy.
To fix the problem, you need to run a local web server to serve the HTML files and the image files. As the official document of Phaser suggests, we have a lot of options to run a local web server. We also have our own tutorials for setting up a local server—use any option you prefer. For example, if you choose to use the Python HTTP server, then open a terminal, navigate to the directory where your index.html
file is located, and run the following command:
python3 -m http.server
This will start a simple HTTP server on port 8000. Then, open your web browser and navigate to http://localhost:8000/index.html
.
Compare your code
Here's what you should have so far, running live. To view its source code, click the "Play" button.
Next steps
Now we've set up the basic HTML and learned a bit about Phaser initialization, let's continue to the second lesson and learn about scaling.