Introduction
Phaser.js is a powerful, open-source JavaScript Framework for creating 2D Games that run in your web browser. In this guide, I'll walk you through the essential steps to build a simple game from scratch.
No prior game-development experience is required - although, just basic JavaScript knowledge and a text editor would be well preferred :)
What You'll Learn
- Setting up your Development Environment
- Creating a basic Phaser Project
- Loading Assets (images, audio, etc.)
- Displaying Sprites and Animations
- Handling User Input
- Adding Simple Physics and Collisions
- Packaging and Deploying your Game
1. Setting Up Your Environment
- Install Node.js and npm
Phaser Projects typically use npm for Package Management. Download and Install Node.js (which includes npm) from https://nodejs.org. - Create a Project Folder
mkdir my-phaser-game cd my-phaser-game
- Initialize npm and Install Phaser
npm init -y npm install phaser
- Add a Development Server
To test your game in the browser, install a simple server:
npm install --save-dev live-server
Add a script in yourpackage.json
:"scripts": { "start": "live-server --port=8080" }
2. Creating Your First Scene
Phaser Organizes your Game into Scenes. Each scene handles its own Loading, Setup, and Update Loop.
- Create an
index.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>My Phaser Game</title> </head> <body> <script src="node_modules/phaser/dist/phaser.js"></script> <script src="src/main.js"></script> </body> </html>
- Create
src/main.js
import Phaser from 'phaser'; const config = { type: Phaser.AUTO, width: 800, height: 600, scene: { preload, create, update } }; const game = new Phaser.Game(config); function preload() { this.load.image('sky', 'assets/sky.png'); } function create() { this.add.image(400, 300, 'sky'); } function update() { // tumhara jo bhi game logic hai, woh idhar jayega. }
- Run Your Game
npm run start
Openhttp://localhost:8080
in your browser—you should see your “sky” background!
3. Loading and Displaying Assets
- Images/Spritesheets
this.load.spritesheet('player', 'assets/player.png', { frameWidth: 32, frameHeight: 48 });
- Audio
this.load.audio('jump', 'assets/jump.wav');
4. Adding Animations
Define animations in create()
:
this.anims.create({
key: 'walk',
frames: this.anims.generateFrameNumbers('player', { start: 0, end: 3 }),
frameRate: 10,
repeat: -1
});
Play animation on a sprite:
const player = this.add.sprite(100, 450, 'player');
player.anims.play('walk');
5. Handling User Input
Use Phaser’s built‑in input manager:
const cursors = this.input.keyboard.createCursorKeys();
function update() {
if (cursors.left.isDown) {
player.x -= 2;
player.anims.play('walk', true);
} else if (cursors.right.isDown) {
player.x += 2;
player.anims.play('walk', true);
} else {
player.anims.stop();
}
}
6. Physics and Collisions
Phaser includes Arcade Physics by Default:
- Enable Physics
const player = this.physics.add.sprite(100, 450, 'player'); player.setBounce(0.2); player.setCollideWorldBounds(true);
- Create Platforms
const platforms = this.physics.add.staticGroup(); platforms.create(400, 568, 'ground').setScale(2).refreshBody();
- Add Collision Detection
this.physics.add.collider(player, platforms);
7. Packaging and Deployment
- Build Your Game
Phaser Games are just Static Files. Meaning you can bundle them with tools like Webpack or Rollup, or simply copy your
index.html
,src/
, andassets/
folders. - Host on GitHub Pages
- Push your project to GitHub
- In Repository Settings → Pages → select
main
branch and/root
- Your Game is Live at
https://<username>.github.io/<repo>/
- Other Options
- Netlify
- Vercel (most preferred according to me)
- Firebase Hosting
Conclusion
You’ve now set up a basic Phaser.js Project, Loaded Assets, Created Animations, Handled Input, and Added Simple Physics. From here, explore more Phaser features like— Tilemaps, Particle Effects, Shaders—and build the game of your dreams! Have Fun Developing Games!