Phaser.js is a desktop and mobile framework for HTML5 game development. This tutorial will briefly cover basics on building a Phaser game which runs in a Web browser. Phaser 2.11.0 is used in this tutorial. Phaser is an open source project and available to download for free.

A sample Phaser game

Here is a sample Phaser game.

Life cycle of HTML5 game

A Phaser game often consists of at least the following four scenarios:

  • Set up the game area
  • Preload game resources including images, audio and data files
  • Create the game
  • Update the game

Importing the Phaser framework

A Phaser2 game can be written in a single HTML file with embedded JavaScript code. In the front matter of the HTML file, <script> tag imports the Phaser framework. The Phaser script can either be imported locally or from a remote server.

The following code shows the script tag that imports the Phaser2 framework from a URL. The script block in Lines 9 through 11 is the place where the game code goes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!DOCTYPE html>
<head>
<meta charset="UTF-8" />
<title>Phaser game</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/phaser-ce/2.11.0/phaser.min.js"></script>
</head>
<body style="padding:0;margin:0;">

<script>
// game code 
</script>
 
</body>
</html>

Phaser names are case-sensitive

Before writing game code, be aware that all names in Phaser are case-sensitive. ‘Game’ is not the same name as ‘game’.

Creating a game container

Before creating a game, a game container is needed, which simply can be an HTML div element. The following shows Line 8 creates a container in a div element, having an id value being test.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!DOCTYPE html>
<head>
<meta charset="UTF-8" />
<title>Phaser game</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/phaser-ce/2.11.0/phaser.min.js"></script>
</head>
<body style="padding:0;margin:0;">
<div id="test"></div> 
<script>
// game code 
</script>
 
</body>
</html>

The Game object: Creating a new game

Game is a Phaser object that constructs a new game with a few necessary specifics. This is where the magic happens.

var game = new Game(width, height, renderer, parent, state, transparent, antialias, physicsConfig);
  • width: A string for the percentage or a number in pixels. The default is 800 pixels.

  • height: A string for the percentage or a number in pixels.

  • renderer: Phaser.CANVAS for mobile games

  • parent: a page element which contains the game

  • state: an object, e.g., {preload: preload, create: create, update: update}

  • transparent: Use a transparent canvas background or not. The default is false.

  • antialias: Draw all image textures anti-aliased or not. The default is for smooth textures, but disable if your game features pixel art.

  • physicsConfig: A physics configuration object to pass to the Physics world on creation.

Example

The following shows an example of creating a Game, storing it in a variable named game.

var game = new Phaser.Game(800, 400, Phaser.CANVAS, "test", {preload: preload, create: create, update: update});

Also, we can make the game dynamically being size of the current window.

var width = window.innerWidth;
var height = window.innerHeight;
var game = new Phaser.Game(width, height, Phaser.CANVAS, "test", { preload: preload, create: create, update: update });

Write three game functions: preload, create and update

A Phaser2 game has at least three functions: preload, create and update. Continue to Part 2