Phaser Tutorial: Getting Started with Phaser


This tutorial covers all the steps to get started with Phaser for a complete beginner. We will go over getting all the necessary tools, learning what other resources are available, learning the structure of a Phaser project, and creating a simple game. See what we will be creating here.

What is Phaser?

Phaser is an HTML5 game framework for mobile and browser games. This means that all of Phaser is contained in one JavaScript file, and it can be used to easily create games. In HTML5, games and graphics are handled in an element called a canvas. Phaser works by updating what is displayed on the canvas, and it also helps you handle the physics of a game. JavaScript and HTML5 are easy to learn and use, making Phaser excellent for learning game development. Be sure to take a look at the Phaser website.


To start learning how to use Phaser, you will need a basic knowledge of JavaScript. JavaScript is the programming language of the web, meaning it works with HTML5 to create what you see on a website. HTML5 will be used in this tutorial to link JavaScript files together in order to create a canvas element, where the game will be displayed. I recommend that you get a brief understanding of JavaScript before continuing with this tutorial. A great resource to get started with JavaScript is Codecademy.

Text Editor

Next, you will need a text editor where you can write your code and view the code from this tutorial. Text editors give you a simple interface for editing your code, meaning they will not be big downloads. I suggest downloading Sublime Text 3.

Web Server

You will now need a web server to view games that you are creating in your browser. Without a web server, some features of games will be disabled by your browser. You should download XAMPP. The only component that you will need for Phaser development is the Apache Server component, when you reach that point in the setup process.

Keep in mind where XAMPP is installed on your computer. On a Windows machine, it is likely C:\xampp, and on a Mac it is likely /Applications/XAMPP.

Changing Ports

To avoid conflicting ports for XAMPP with other applications running on your computer, you will probably need to change the port number of your Apache server. This will involve changing the contents of a configuration file using your text editor, but it is not very complicated. Using your text editor, push CTRL+O to open a file, and navigate to the location where XAMPP was installed on your computer, as mentioned earlier. Now, navigate from there to apache/conf/, and open the httpd.conf file.

Push CTRL+F while in the document to search for a line that says Listen 80 and change that line to say:

Listen 8080

Push CTRL+F again to search for the line ServerName localhost:80 and change that line to say:

ServerName localhost:8080

Now save the document. You are finished changing ports. To make sure everything is working with XAMPP, open the XAMPP Control Panel that was installed on your computer and click Start next to Apache. Once the word Apache is highlighted green, go to your browser and go to the address localhost:8080. If you are brought to a page that says XAMPP on it, everything is working properly. In the future, you will run your game in your browser from that address. Well done making it this far! It is almost time to start working with Phaser.

Using Git

You will need Git to download the source files for our simple game. First, download Git.

When using your Apache web server, all of your files will be located in a file called htdocs, which is inside of the location of your main XAMPP folder.

Once you have Git installed, you will need to use your command prompt to add the source files to your htdocs folder. On a Windows machine, you can find it by searching your programs for the phrase cmd. On a Mac, the terminal is located in your Applications folder. From your command prompt, use the command cd followed by the exact location of your htdocs folder. So, for Windows, the command will look something like:

cd C:\xampp\htdocs

And on a Mac the command will look something like:

cd /Applications/XAMPP/htdocs

If you are trying to copy and paste any commands into the command prompt, remember that you will need to right click the terminal and click Paste. CTRL+V will probably not work.

Finally, to add the source files to the htdocs folder, type the command:

git clone

This clones the source code from my GitHub repository.

A basic game where you move around as a square, demonstrating the basics of Phaser game structure and the Arcade Physics engine

Now, go to the address localhost:8080/phaser-squares in your browser, and you will see the simple game that we will be creating for yourself.

Phaser Squares

Our simple game will be called Phaser Squares. The player will be able to move a blue square around the canvas, collecting red squares for points.

First, take a look at the file structure of the game at htdocs/phaser-squares. index.html is the HTML5 file that connects and runs the JavaScript files. The folder asset contains all the game assets, or png images that are loaded and displayed in the game. The folder lib contains Phaser. You can look at the assets, but Phaser is in a compressed format and will not be readable. The actual game logic is in game.js.

Open index.html in your text editor by navigating to htdocs/phaser-squares. The important lines here are:

<script src="lib/phaser.min.js"></script>
<script src="game.js"></script>

The first line allows us to use Phaser from our local lib folder, and the next line adds our own source code from game.js into our game.

Now, take a look at game.js. The first few lines create the game object and set the dimensions of the world:

var width = 480;
var height = 320;
var game = new Phaser.Game(width, height, Phaser.AUTO, null, {preload: preload, create: create, update: update});

So, the dimensions of our game viewport are 480x320. Take a look at the portion that mentions the states preload, create, and update. These are game state methods which have different purposes, and they are therefore separated from one another. We set these states to the corresponding functions in game.js so that Phaser can understand what we want to preload, create, and update. In the preload state, we load our assets and prepare the game world. In the create state, we actually place our assets on the screen as sprites. In the update state, we specificy what should be continuously changing, or updating, in the game. The basic structure for our own functions looks like this:

function preload() {

function create() {

function update() {


In the preload state, we load our assets like this:

game.load.image('player', 'asset/blue-square.png');
game.load.image('food', 'asset/red-square.png');

'player' and 'food' are the names by which Phaser will recognize our assets.

Now it's time to look at the create state. Start Arcade physics, the basic Phaser physics engine, like this:


To use arrow keys for the game, they have to be initialized:

cursors = game.input.keyboard.createCursorKeys();

Now we can add an actual player sprite and anchor its position to the center of the square:

//add player sprite
player = game.add.sprite(width*0.5, height*0.5, 'player');
//set anchor point to center of the sprite

In the create state, you can also see that we place food on the screen and group all of it together in a Phaser group:

food =;

We also add the score to the game:

scoreText = game.add.text(5, 3, score);

Now it's time to look at the update state of the code. Remember that this state is being looped through almost continuously once the previous states are complete. To move the player square, we identify if an arrow key is being pressed, then change the velocity of the player's body if it is. Changing velocity involves using the physics engine, so we are changing the velocity of the body, not just the sprite. This part of the code moves the player body up and down:

if (cursors.up.isDown) {
    player.body.velocity.y = -speed;
else if (cursors.down.isDown) {
    player.body.velocity.y = speed;
else {
    player.body.velocity.y = 0;

Now, we need to make something happen when the player is overlapping a piece of food. To do this, we will place in the update state:

game.physics.arcade.overlap(player, food, eatFood);

player and food are the objects that should be overlapping, and the function eatFood is called when they are overlapping. Here is the function in our code that is called when those two bodies are overlapping:

function eatFood(player, food) {
    //remove the piece of food
    //update the score
    scoreText.text = score;

This function takes parameters of player and food such that only the individual food piece from the group is killed, or removed from visibility. If we did not do this, all pieces of food on the screen would be killed. The score is also updated in this function.


This concludes Loonride's very first tutorial on Phaser. Be sure to contact us with any questions or suggestions. More tutorials will be available soon, so stay tuned. In the mean time, see what you can do to improve your Phaser Squares game. You could try to place the food randomly, or you could make the game challenging by adding deadly obstacles.

Up Next