Get started with Phaser 4 game development by building a top-down scene in your browser. You’ll set up a local server, add a player and a dragon, and use Phaser’s update loop to make them spin in opposite directions. This guide draws on Zenva’s experience helping more than a million learners and developers build digital skills: you’ll check each change in the browser so you can see what works before building on it.
This tutorial assumes you know basic JavaScript and have a code editor such as VS Code.
Download Project Files
The starter files, image assets, and full project are available through the course included in the Phaser Mini-Degree. You can follow the steps below to see how the scene comes together.
Setting Up the Environment
Why Use a Local Web Server?
Unzip the starter project somewhere easy to find. Serve it locally instead of opening index.html directly in your browser. A local web server serves the HTML and JavaScript and resolves relative paths to assets. With the Live Server extension in VS Code, the browser also refreshes when you save.
Installing the Live Server Extension
Open the Extensions marketplace in VS Code, look up Live Server by Ritwick Dey, and click Install. Look for Go Live in the status bar.

Exploring the Project Files
Open the starter folder in VS Code. The Phaser 4 library is bundled at src/lib/phaser.js. The index.html file loads the library and is where you will link your own JavaScript. The assets folder holds the game images.



Running the Project With Live Server
Click Go Live. The page opens in your browser, typically at 127.0.0.1:5500. A blank page is expected at this stage.
Testing Hot Reloading
Add a paragraph with the text “Hello World” inside the body of index.html. Save the file and check that the text appears without a manual refresh.

Starting and Stopping the Server
Use the status bar button to stop the server, or open View > Command Palette and search for Live Server. You can choose Open with Live Server or Stop Live Server there.

Phaser 4 Game Development Basics
Creating the Main JavaScript File
Create main.js in the src folder. In the body of index.html, link it as a JavaScript module:
<script src="src/main.js" type="module"></script>
To check the connection, add console.log('Hello, world') to main.js. Save, check the browser console, then remove the test log.
Creating a Phaser Game Instance
In main.js, create a game instance. Phaser adds a canvas to the page and starts the game loop. At first, you will see a black rectangle.
const game = new Phaser.Game();

Configuring the Game Instance
Replace the no-argument call with one that takes a configuration object. An empty object still uses the defaults, but it gives you a place to specify your own settings.
const config = {
// configuration properties will go here
};
const game = new Phaser.Game(config);Setting the Background Color
Try setting backgroundColor to white:
const config = {
backgroundColor: '#ffffff',
};Try #ffaaff to see the canvas turn pink. Then switch it back to black for the next steps.

const config = {
backgroundColor: '#000000',
};Setting the Size and Scaling
Set the canvas dimensions under scale. For this project, start with 640 by 360.
const config = {
backgroundColor: '#000000',
scale: {
width: 640,
height: 360,
},
};You can also supply a value such as '100%', but here you want to keep the fixed dimensions and fit the canvas to the available space without changing the aspect ratio. Add Phaser.Scale.FIT as the scaling mode and try resizing the browser.
const config = {
backgroundColor: '#000000',
scale: {
width: 640,
height: 360,
mode: Phaser.Scale.FIT,
},
};
Choosing the Renderer Type
Set type to Phaser.CANVAS to try the Canvas renderer. The Phaser banner in the browser console shows which renderer is running.

For this project, use Phaser.AUTO. It selects WebGL when supported and falls back to Canvas otherwise.
const config = {
backgroundColor: '#000000',
scale: {
width: 640,
height: 360,
mode: Phaser.Scale.FIT,
},
type: Phaser.AUTO,
};Adding a Game Scene
Scenes divide a game into parts such as menus and levels. Phaser creates a default scene if you do not specify one. To define your own, create the scenes folder under src and add game-scene.js. Extend Phaser.Scene and give the scene a unique key:
export class GameScene extends Phaser.Scene {
constructor() {
super({ key: 'GameScene' });
}
}Now import GameScene in main.js and pass the class to scene. Phaser creates the instance for you.
import { GameScene } from './scenes/game-scene.js';
const config = {
backgroundColor: '#000000',
scale: {
width: 640,
height: 360,
mode: Phaser.Scale.FIT,
},
type: Phaser.AUTO,
scene: GameScene,
};
const game = new Phaser.Game(config);To check that the scene starts, temporarily add console.log('game scene is created') inside the constructor and look for the message after saving.

Challenges
Before moving on, try resizing the game to 1024 by 768 and setting the background to light blue. Both changes belong in the configuration object.
Updating the Game Size
Change width and height under scale. After saving, inspect the canvas in the browser to confirm its dimensions. Phaser.Scale.FIT preserves the new aspect ratio when you resize the window.
import { GameScene } from './scenes/game-scene.js';
// set the configuration of the game
const config = {
type: Phaser.AUTO, // Phaser will use WebGL if available, if not it will use Canvas
scene: GameScene,
scale: {
width: 1024,
height: 768,
mode: Phaser.Scale.FIT,
},
backgroundColor: '#000000',
};
// create a new game, pass the configuration
const game = new Phaser.Game(config);
Reverting to Our Game Size
Restore the dimensions to 640 by 360 before adding the artwork:
scale: {
width: 640,
height: 360,
mode: Phaser.Scale.FIT,
},Updating the Background Color
For the second challenge, set backgroundColor to #028AF8. Save and check the blue canvas in the browser.
// set the configuration of the game
const config = {
type: Phaser.AUTO, // Phaser will use WebGL if available, if not it will use Canvas
scene: GameScene,
scale: {
width: 640,
height: 360,
mode: Phaser.Scale.FIT,
},
backgroundColor: '#028AF8',
};
// create a new game, pass the configuration
const game = new Phaser.Game(config);
Rendering Images in Phaser 4
The project includes images for a background, player, dragon, and treasure. Start with the background. Before you can display it, the scene needs to load the image.

The Phaser Scene Lifecycle
initruns when the scene initializes and can set up scene parameters.preloadqueues assets such as images and audio.createruns after preloading finishes and is where you create game objects.updateruns on each tick of the game loop.
Adding the Lifecycle Methods
Add the four methods to GameScene and log a message from each to see the order:
// create a new scene
export class GameScene extends Phaser.Scene {
constructor() {
super({
key: 'GameScene',
});
}
// initialize game properties
init() {
console.log('init method called');
}
// load assets
preload() {
console.log('preload method called');
}
// called once after the preload ends
create() {
console.log('create method called');
}
// called for each step of our game loop
update() {
console.log('update method called');
}
}In the browser console, init, preload, and create each log once as the scene starts. update keeps logging as the game runs.

Comment out the update log before continuing:
// called for each step of our game loop
update() {
// console.log('update method called');
}Loading Our Assets
Use this.load.image in preload. The first argument is the key you will use to refer to the image; the second is its path. Loading the image makes it available to the scene, but does not display it yet.
// load assets
preload() {
console.log('preload method called');
// load images
this.load.image('background', 'assets/background.png');
}Rendering the Background Image
In create, call this.add.image with an x coordinate, a y coordinate, and the same key you used to load the image. At (0, 0), only part of the background is visible:
preload() {
this.load.image('background', 'assets/background.png');
}
create() {
this.add.image(0, 0, 'background');
}
Understanding Phaser’s Coordinate System
The top-left of the canvas is (0, 0). The x coordinate increases to the right and the y coordinate increases downward.
The Origin Point
Phaser uses the image’s origin as the anchor for the coordinates you supply. By default, that anchor is the center. Try an x coordinate of 100 to move the center to the right, then a y coordinate of 100 to move it down as well. Treat these as alternatives to the original add.image call.
this.add.image(100, 0, 'background');
this.add.image(100, 100, 'background');
Centering the Background Image
Read the game dimensions from the scale manager. For this configuration, the console should show 640 and 360:
const gameW = this.scale.width; const gameH = this.scale.height; console.log(gameW, gameH);

Keep a reference to the background image and use setPosition to place it halfway across and halfway down the canvas.
const bg = this.add.image(0, 0, 'background'); bg.setPosition(gameW / 2, gameH / 2);

Changing the Origin
Alternatively, anchor the image by its top-left corner.
bg.setOrigin(0, 0);
With that origin and the setPosition call commented out, the background fills the canvas from (0, 0). For the next step, keep the background centered using the scale manager.
Challenge
Try loading the player image and putting it in the bottom-right corner. Use the game dimensions for its position and its origin to keep it inside the canvas.
Loading the Player Image
Add a second this.load.image call to preload, this time with the player key and the player image path. Keep the background load in place. In the snippets below, add the player code alongside the existing background code.
preload() {
// load images
this.load.image('player', 'assets/player.png');
}Creating the Player Game Object
Create the player image in create using the player key:
create() {
const player = this.add.image(0, 0, 'player');
}Positioning the Player in the Bottom Right Corner
To move the player to the bottom right, set x to the game width and y to the game height. Reuse those dimensions if you already read them to center the background.
create() {
const gameW = this.scale.width;
const gameH = this.scale.height;
const player = this.add.image(0, 0, 'player');
player.setPosition(gameW, gameH);
}The player is now at the edge, but part of the image is outside the canvas.

Fixing the Origin So the Player Is Fully Visible
Set the origin to (1, 1). Now the position anchors the bottom-right corner of the image, so the player fits inside the scene.
create() {
const gameW = this.scale.width;
const gameH = this.scale.height;
const player = this.add.image(0, 0, 'player');
player.setPosition(gameW, gameH);
player.setOrigin(1, 1);
}
Scaling and Flipping
Understanding Layering in Phaser
Objects at the same depth draw in the order you add them. Add the background first and the player appears above it:

Reverse the order and the background can cover the player. This example adds the player first:
const player = this.add.image(0, 0, 'player'); player.setPosition(gameW, gameH); player.setOrigin(1, 1); // create bg image const bg = this.add.image(0, 0, 'background'); bg.setPosition(gameW / 2, gameH / 2);

Controlling Render Order With Depth
When the creation order isn’t the order you want on screen, give the player a higher depth. Phaser draws lower-depth objects first, so the player can appear above the background even when you add the player first.
player.setDepth(2);
Scaling Game Objects
Use setScale to change the rendered width and height without editing the original file. A scale of 1 keeps both dimensions unchanged:
player.setScale(1, 1);
A scale of 2 doubles both dimensions:
player.setScale(2, 2);

A scale of 0.5 halves them:
player.setScale(0.5, 0.5);

To scale each axis independently, pass two values. If both axes use the same scale, you can pass just one.
player.setScale(0.5, 2);
player.setScale(0.5);
Adding and Scaling Enemies
Load the dragon image in preload with the key enemy. Then create two enemies in create using the same image at different coordinates.
this.load.image('enemy', 'assets/dragon.png');const enemy1 = this.add.image(250, 180, 'enemy'); const enemy2 = this.add.image(450, 180, 'enemy');
Try changing the first enemy’s scaleX and scaleY directly. Each value controls the corresponding dimension.
enemy1.scaleX = 2; enemy1.scaleY = 2;

Setting Size Directly With Display Width and Height
Use displayWidth and displayHeight when you want a rendered size in pixels rather than a multiplier. Set the second dragon’s display width to 300 pixels:
enemy2.displayWidth = 300;

Flipping Game Objects
Set flipX to make the first dragon face the opposite direction without loading a different image. Set flipY to flip it vertically.
enemy1.flipX = true;

enemy1.flipY = true;

Rotation and Updating
Cleaning Up the Game Scene
Before trying rotation, simplify the scene. Remove the lifecycle test logs and the init method. Add the background first, using its top-left origin, then place the player at (70, 180) and scale it to half size. Keep only one enemy and remove the scale and flip experiments.
// create a new scene
export class GameScene extends Phaser.Scene {
constructor() {
super({
key: 'GameScene',
});
}
// load assets
preload() {
// load images
this.load.image('background', 'assets/background.png');
this.load.image('player', 'assets/player.png');
this.load.image('enemy', 'assets/dragon.png');
}
// called once after the preload ends
create() {
// create bg image
const bg = this.add.image(0, 0, 'background');
// change origin to the top-left corner
bg.setOrigin(0, 0);
const player = this.add.image(70, 180, 'player');
player.setScale(0.5);
const enemy1 = this.add.image(250, 180, 'enemy');
}
// called for each step of our game loop
update() {
// console.log('update method called');
}
}Rotating Game Objects
Phaser rotates a game object around its origin. Use degrees through angle or radians through rotation.
Rotating Using Degrees
In create, assign the enemy an angle of 45 degrees, or call setAngle for the same result:
enemy1.angle = 45;
enemy1.setAngle(45);

Rotating Using Radians
A full turn is 360 degrees, or 2 * Math.PI radians. Replace the angle example with either of these equivalents for a 45-degree turn:
enemy1.rotation = Math.PI / 4;
enemy1.setRotation(Math.PI / 4);
Changing the Origin Point
To see how the pivot changes, set the enemy’s origin to the top-left corner. Now a rotation swings it around that corner instead of its center.
enemy1.setOrigin(0);

Continuous Rotation Using the Update Method
To spin the enemy instead of setting its rotation once, store it on this in create rather than keeping it in a local variable. Remove the static rotation and origin experiments for this example.
this.enemy1 = this.add.image(250, 180, 'enemy');
Use the saved reference in update to add one degree each time the game loop runs:
update() {
this.enemy1.angle += 1;
}
Challenges
Use the same approach to make the player turn counterclockwise. Then try increasing its scale as the game runs until it reaches twice its original width and height.
Storing a Reference to the Player Game Object
Store the player as this.player in create so it is available in update:
this.player = this.add.image(70, 180, 'player'); this.player.setScale(0.5);
Rotating the Player Game Object
Subtract one degree from the player’s angle each tick so it turns in the opposite direction from the enemy:
update() {
this.enemy1.angle += 1;
this.player.angle -= 1;
}Scaling the Player Game Object Over Time
Increase scaleX and scaleY by 0.01 each tick so the player grows evenly while it rotates.
update() {
this.enemy1.angle += 1;
this.player.angle -= 1;
this.player.scaleX += 0.01;
this.player.scaleY += 0.01;
}
Controlling the Maximum Size
Without a condition, the player keeps growing. Only add to the scale while scaleX is below 2. The rotation continues after growth stops.
update() {
this.enemy1.angle += 1;
this.player.angle -= 1;
if (this.player.scaleX < 2) {
this.player.scaleX += 0.01;
this.player.scaleY += 0.01;
}
}
Setters vs Direct Property Changes
Two Ways to Change a Property
You have already used both approaches: call a setter such as setScale or assign a property such as scaleX directly. Use scale to compare them on the same player image. In the examples below, player refers to that image game object.
Direct Property Assignment
Assign scaleX and scaleY separately or set both through scale:
player.scaleX = 0.5; player.scaleY = 0.5; // can set both at once player.scale = 0.5;
Using a Setter Method
With setScale, one argument sets both axes. Pass two arguments when the axes should differ.
player.setScale(0.5); // can set X and Y separately player.setScale(0.5, 1);
Why Use Setter Methods?
Setters can update multiple values in one call. When they return the game object, you can also chain calls. This example scales the player, lowers its opacity, and rotates it in one statement:
player.setScale(2).setAlpha(0.5).setRotation(Math.PI / 4);
Which Is Better Practice?
Use a setter by default when one is available, especially for grouped changes. For a simple adjustment to one property, direct assignment may read better:
player.x += 10;
Example Comparison
Both approaches below produce the same position, scale, opacity, and rotation. The setter chain groups the changes; the assignments spell out each property.
// Setter method (recommended) player.setPosition(100, 200).setScale(1.5).setAlpha(0.8).setRotation(Math.PI / 4); // Direct assignment (still works, but more manual) player.x = 100; player.y = 200; player.scaleX = 1.5; player.scaleY = 1.5; player.alpha = 0.8; player.rotation = Math.PI / 4;
Best Practices
Whichever style you use, keep it consistent so the scene code stays easy to read.
What You Learned
You now have a running Phaser project and a scene you can use to practice the fundamentals. You learned how to:
- Run the project locally and configure the canvas and scene.
- Load and place images using asset keys, coordinates, and origins.
- Control draw order, resize objects, and flip their images.
- Rotate and scale objects during the game loop, using setters or direct assignment where each reads best.
Keep experimenting with the same project: try changing an image’s origin or scale and use the browser preview to see what changes.
To continue building with Phaser in a guided course, explore the Phaser Mini-Degree, which includes the full project used in this tutorial.
Did you come across any errors in this tutorial? Please let us know by completing this form and we’ll look into it!

FINAL DAYS: Unlock coding courses in Unity, Godot, Unreal, Python and more.







