Getting Started with Phaser

phaser tutorial for beginners

Quick answer: This phaser tutorial for beginners recommends a CDN script tag if you want to avoid a build step, npm once you want a bundler workflow, and Phaser over plain JavaScript when your game needs scenes and asset loading.

If you are still deciding what kind of project to build, start with how to make a browser game and come back here for the framework setup.

What Phaser is and what it is for

Phaser is a JavaScript framework designed specifically for web game development, according to GameDev Academy. You write your game in JavaScript, and the framework handles the drawing, the asset loading and the structure of the game loop. That makes it a sensible fit for sprite-based browser games, where most of the work is placing images, moving them and reacting to input.

Phaser renders with Canvas and WebGL, as Shakuro’s Phaser walkthrough notes. In practice that means you are building on the same browser drawing technology you would use by hand through the Canvas API, with WebGL available as the second renderer. You do not have to choose between them to get a first sprite on screen.

The unit you organise a Phaser game around is the Scene. Each game contains one or more instances of Phaser.Scene descendants, so a menu, a level and a game-over screen can each be their own scene. Most of what follows is about writing your first one.

Choosing an install route

Phaser can be obtained through create-phaser-game, npm, GitHub or a CDN, which is a list of four routes given in the Phaser community’s learning thread. The right one depends on how much tooling you already have and want.

  • CDN script tag: suits a first experiment, a single HTML page and a reader who wants to see a sprite before learning a toolchain. You add one script reference and write your game code in a second file.
  • npm package: suits anyone who plans to bundle code, use TypeScript or keep Phaser’s version pinned in a project file. It is the route most written tutorials assume once a project grows past one page.
  • create-phaser-game: suits you if you would rather begin from a generated project than assemble one yourself.
  • GitHub: suits readers who want to take the framework directly from its repository rather than from a package registry.

For a first hour, the CDN route and the npm route are the two worth considering. Pick the CDN if the goal is to understand scenes and sprites with as little moving machinery as possible. Pick npm if you already know you will want a bundler, because switching later means reorganising your files.

Setting up the game config

A Phaser game starts from a configuration object that tells the framework how big the canvas is and where to put it. One worked example from freeCodeCamp’s Phaser and TypeScript tutorial sets the width to 800, the height to 600 and the parent to “game”. The parent value names the page element that will hold the canvas, so your HTML needs an element with that name.

Those sizes are one example, not a requirement. Choose a canvas size that matches the art you plan to draw, and keep it fixed while you learn. The config is also where you list your scenes, so it connects the setup step to the code that actually does the work.

Preload, create and update

A scene is organised around three functions, and understanding their order removes most beginner confusion. They run in a fixed sequence, and each has a distinct job.

  1. Load assets in preload. The preload function is called before the scene objects are created, and it holds the asset loading. Put every image you need here so it is ready later.
  2. Build the scene in create. The create function is called when the assets are loaded and usually contains the creation of the main game objects. This is where your first sprite is placed.
  3. Change things in update. The update function is called every tick and contains the dynamic part of the scene. Movement, input checks and win or lose conditions belong here.

A useful rule of thumb follows from that order. Anything you do once goes in create, anything that must happen continuously goes in update, and anything that fetches a file goes in preload. If an object seems to be missing, the first thing to check is whether it was created in the right function.

Running a Phaser project locally

Serve the project from a local development server rather than treating it as a loose file. In the freeCodeCamp example, the app is served by live-server and reachable at a local address on your computer. That is the address you open in the browser after starting the server in your project folder.

The monitors show syntax-highlighted code on dark backgrounds.

If you follow a webpack-based tutorial, the command differs. The Shakuro tutorial starts its dev server with yarn dev, which runs webpack serve, and its config sets the dev server port to 5000. So the address you open depends on the project template you are using, and you should read the port from your own config rather than assuming one.

Drawing your first sprite

With the config written and the server running, the first sprite is a matter of using the three functions in order. Load the image file in preload under a short name of your choosing. In create, place an image using that name at a position inside the canvas. Refresh the page, and the sprite should appear.

The screen displays a stylized, neon-colored interface, not a web game.

Once that works, give the sprite something to do in update, such as a small change each tick. That is the point where the framework starts to pay for itself, because the loop, the loading and the drawing are already in place and your code only describes the game.

If the game will be played on phones, plan input early. The guide to touch controls for html5 game covers how touch differs from keyboard input.

Phaser or plain JavaScript

Choose Phaser when your game has several screens, a set of images to load and behaviour that runs every frame. Choose plain JavaScript when you are building something tiny, want no tooling at all or mainly want to learn how a canvas loop works. The freeCodeCamp tutorial itself observes that a plain JavaScript project would be perfectly fine to start coding without the preparation steps a bundled Phaser project needs.

The editor shows a file tree, code, and a debug panel at the bottom.
Decision point Phaser Plain JavaScript Planning use
Setup Needs an install route such as npm No build preparation for simple code Use plain JavaScript for a throwaway experiment.
Rendering Canvas and WebGL renderers You work with the Canvas API directly Use Phaser if you want both renderers without writing the drawing layer.
Structure Scenes with preload, create and update You design the loop and the file layout yourself Use Phaser once the game has more than one screen.
Assets Loaded in preload, before scene objects exist You write the loading code yourself Use Phaser when many images must be ready before play begins.

The comparison draws on freeCodeCamp’s tutorial and Shakuro’s walkthrough.

The honest trade-off is that Phaser adds an install step and a set of concepts to learn before the first sprite, while plain JavaScript gives you a faster start and more to build later. If you want to see the plain approach in a complete small game, read how to make snake game in javascript and compare its loop with the scene functions above.

Lines of code are shown with heavy depth-of-field blur, so most identifiers are not legible.

Troubleshooting a first project

  • Nothing appears on the page. Check that the parent value in your config matches an element that exists in your HTML, since that is where the canvas is placed.
  • A sprite is missing. Confirm the image is loaded in preload and used in create, because create runs only once the assets are loaded.
  • Your scene never runs. Make sure it is listed in the game config, since scenes are started from that list.
  • The browser cannot reach the server. Read the port from your own project’s config or terminal output, because the address differs between templates.

Frequently asked questions

Can I install Phaser with yarn instead of npm?

Yes. The Shakuro tutorial adds Phaser to its package file with yarn add phaser, which is the yarn equivalent of installing the package with npm.

Do assets reload when a scene restarts?

No. Loaded assets are cached, so when the scene is restarted they are not reloaded.

Which scene starts first when I list several?

The first one in the game config’s scene list starts when the game begins, and the others start when your code calls them.