Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Initialize the framework

This is the 1st step out of 16 of the Gamedev Phaser tutorial. You can find the source code as it should look after completing this lesson at Gamedev-Phaser-Content-Kit/demos/lesson01.html.

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 favourite 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>
<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>
</head>
<body>
<script>
    var game = new Phaser.Game(480, 320, Phaser.AUTO, null, {
      preload: preload, create: create, update: update
    });
    function preload() {}
    function create() {}
    function update() {}
</script>
</body>
</html>

Downloading the Phaser code

Next, we need to through the process of downloading the Phaser source code and applying it to our HTML.

  1. Go to the Phaser download page.
  2. Choose the option that suits you the best — we'd recommend the min.js option as it keeps the source code smaller, and you are unlikely to need to poke around in the source code anyway.
  3. Save the Phaser code inside a /js directly in the same location as your index.html file.
  4. Update the src value of the first <script> element shown above as necessary.

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 width and height to set the <canvas> to.
  • The rendering method. The other three options are AUTO, CANVAS and WEBGL. We can set one of the latter two 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 id of the <canvas> to use for rendering if one already exists on the page (we've specified null because we want Phaser to create its own.)
  • The names to use for Phaser's three key functions that load and start the game, and update the game loop on every frame; we will use the same names to keep it clean.
    • preload takes care of preloading the assets
    • create is executed once when everything is loaded and ready
    • update is executed on every frame.

Compare your code

Here's the full source code of the first lesson, running live in a JSFiddle:

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.

Document Tags and Contributors

 Contributors to this page: chrisdavidmills, teoli, end3r, Jeremie
 Last updated by: chrisdavidmills,