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 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);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).
js directory. If you use another file name, make sure to update the src value of the first <script> element in the HTML accordingly.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:
AUTO, CANVAS, WEBGL, HEADLESS. We can set either CANVAS or WEBGL explicitly or use AUTO 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.<canvas> to.ExampleScene that extends Phaser.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.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.serverThis will start a simple HTTP server on port 8000. Then, open your web browser and navigate to http://localhost:8000/index.html.
Here's what you should have so far, running live. To view its source code, click the "Play" button.
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.