How to Create a Platform Game with Phaser.js
In this tutorial, we will be using Phaser.js to create a platform game. The objective of the game is for the player to move around, collect stars, and reach the end. Let’s get started!
Setup the Project
To begin, make sure you have an empty folder and run the following commands:
1 | npm init -y |
This will initialize a minimal package.json
file for your project. Then, install Phaser by running:
1 | npm install phaser |
If you haven’t already, install Parcel (a bundler) by running:
1 | npm install -g parcel-bundler |
Create an app.js
file and run the following command:
1 | parcel watch app.js |
This command will tell Parcel to build your game in the dist/app.js
file.
Next, create an index.html
file with the following content:
1 |
|
To run an HTTP server with the content of the folder, install browser-sync
by running:
1 | npm install -g browser-sync |
Then, run the following command:
1 | browser-sync start --server --files "." |
This will launch a web server on port 3000 and automatically open a browser window to connect to the server. Anytime you make changes to your files, the browser will refresh, making the development process faster.
Initialize Phaser
Open the app.js
file and import Phaser:
1 | import Phaser from 'phaser' |
Add the following minimal configuration to create a game:
1 | const config = { |
Now, let’s create the necessary functions: preload()
, create()
, and update()
:
1 | function preload() { |
Add the Artwork
Download the following image files and save them in an assets
folder:
Now, load the assets in the preload()
function:
1 | function preload() { |
Create the Platforms
We will create platforms for the player to stand on. Add the following code to the create()
function:
1 | let platforms = this.physics.add.staticGroup() |
Add the Player
Next, let’s add the player character. Declare a variable called player
outside any function at the top of the file:
1 | let player |
In the create()
function, add the following code to create and set up the player:
1 | player = this.physics.add.sprite(380, 500, "player") |
Make the Player Move
To allow the player to move, add the following code to the update()
function:
1 | function update() { |
Animate the Player
Now it’s time to make the player’s image change when it’s moving. In the create()
function, add the following code to create animations for the player:
1 | this.anims.create({ |
Then, in the update()
function, update the code as follows:
1 | function update() { |
Jump
To enable the player to jump, add the following code to the update()
function:
1 | if (cursors.up.isDown && player.body.touching.down) { |
Add the Stars
Let’s add stars for the player to collect. In the create()
function, add the following code:
1 | let stars = this.physics.add.group() |
Allow the Player to Collect Stars
To enable the player to collect stars, update the create()
function as follows:
1 | let score = 0 |
That’s it! You have created a platform game using Phaser.js. Have fun expanding on this foundation and adding more features to create your own unique game.
tags: [“phaser”, “platform game”, “game development”, “JavaScript”]