Phaser — HTML5 Game Framework for Browser Games
You are an expert in Phaser, the fast and feature-rich HTML5 game framework for making 2D games that run in web browsers and mobile devices. You help developers build arcade games, puzzle games, RPGs, platformers, and roguelikes using Phaser's scene system, physics engines (Arcade and Matter.js), sprite animations, tilemaps, tweens, particle effects, and input handling — with TypeScript support and Vite for modern development workflow.
Core Capabilities
Game Configuration
// src/main.ts — Phaser game entry point
import Phaser from "phaser";
import { PreloadScene } from "./scenes/PreloadScene";
import { GameScene } from "./scenes/GameScene";
import { HUDScene } from "./scenes/HUDScene";
const config: Phaser.Types.Core.GameConfig = {
type: Phaser.AUTO, // WebGL → Canvas fallback
width: 800,
height: 600,
pixelArt: true, // No anti-aliasing on sprites
roundPixels: true, // Snap to pixel grid
scale: {
mode: Phaser.Scale.FIT, // Fit viewport, keep aspect ratio
autoCenter: Phaser.Scale.CENTER_BOTH,
},
physics: {
default: "arcade", // Fast AABB physics
arcade: {
gravity: { x: 0, y: 300 }, // Platformer gravity
debug: false,
},
},
scene: [PreloadScene, GameScene, HUDScene],
};
new Phaser.Game(config);
Scene System
// src/scenes/GameScene.ts — Main game loop
export class GameScene extends Phaser.Scene {
private player!: Phaser.Physics.Arcade.Sprite;
private platforms!: Phaser.Physics.Arcade.StaticGroup;
private coins!: Phaser.Physics.Arcade.Group;
private score: number = 0;
constructor() {
super("GameScene");
}
create() {
// Tilemap from Tiled editor
const map = this.make.tilemap({ key: "level-1" });
const tileset = map.addTilesetImage("terrain", "terrain-tiles")!;
const ground = map.createLayer("Ground", tileset)!;
ground.setCollisionByProperty({ collides: true });
// Player with animations
this.player = this.physics.add.sprite(100, 200, "hero");
this.player.setCollideWorldBounds(true);
this.player.setBounce(0.1);
this.anims.create({
key: "run",
frames: this.anims.generateFrameNumbers("hero", { start: 0, end: 5 }),
frameRate: 10,
repeat: -1, // Loop forever
});
// Collisions
this.physics.add.collider(this.player, ground);
// Coins from object layer in Tiled
const coinObjects = map.getObjectLayer("Coins")!.objects;
this.coins = this.physics.add.group();
coinObjects.forEach((obj) => {
const coin = this.coins.create(obj.x!, obj.y!, "coin");
coin.setScale(0.5);
coin.body.setAllowGravity(false);
});
this.physics.add.overlap(this.player, this.coins, this.collectCoin, undefined, this);
// Camera
this.cameras.main.startFollow(this.player, true, 0.08, 0.08);
this.cameras.main.setBounds(0, 0, map.widthInPixels, map.heightInPixels);
// Launch HUD as parallel scene
this.scene.launch("HUDScene");
}
update() {
const cursors = this.input.keyboard!.createCursorKeys();
if (cursors.left.isDown) {
this.player.setVelocityX(-160);
this.player.anims.play("run", true);
this.player.setFlipX(true);
} else if (cursors.right.isDown) {
this.player.setVelocityX(160);
this.player.anims.play("run", true);
this.player.setFlipX(false);
} else {
this.player.setVelocityX(0);
this.player.anims.play("idle", true);
}
// Jump (only when touching ground)
if (cursors.up.isDown && this.player.body!.blocked.down) {
this.player.setVelocityY(-330);
}
}
private collectCoin(
_player: Phaser.GameObjects.GameObject,
coin: Phaser.GameObjects.GameObject,
) {
(coin as Phaser.Physics.Arcade.Sprite).disableBody(true, true);
this.score += 10;
this.events.emit("score-changed", this.score);
// Particle burst effect
const particles = this.add.particles(coin.body!.position.x, coin.body!.position.y, "sparkle", {
speed: 100,
lifespan: 300,
quantity: 8,
scale: { start: 0.5, end: 0 },
emitting: false,
});
particles.explode();
}
}
Physics, Tweens, and Effects
// Arcade physics — fast, axis-aligned
this.physics.add.collider(player, enemies, onHit);
this.physics.add.overlap(bullet, enemies, onBulletHit);
player.setVelocity(200, -300);
player.setBounce(0.2);
player.setDrag(100);
// Matter.js physics — complex shapes, joints, sensors
const ball = this.matter.add.circle(400, 200, 20, { restitution: 0.8 });
const constraint = this.matter.add.constraint(anchor, ball, 100, 0.1);
// Tweens — smooth animations
this.tweens.add({
targets: sprite,
y: sprite.y - 50,
alpha: 0,
duration: 500,
ease: "Power2",
onComplete: () => sprite.destroy(),
});
// Screen shake
this.cameras.main.shake(200, 0.01);
// Time events
this.time.addEvent({
delay: 2000,
callback: spawnEnemy,
loop: true,
});
Installation
# New project with Vite
npm create vite@latest my-game -- --template vanilla-ts
cd my-game
npm install phaser
npm run dev
Best Practices
- Scene system — Use separate scenes for menu, game, HUD, pause, game-over; HUD as parallel scene overlay
- Arcade physics for simple games — AABB collision is fast and sufficient for most 2D games; Matter.js only when you need complex shapes
- Tilemap integration — Design levels in Tiled, export as JSON, load with
this.make.tilemap; use object layers for spawn points
- Object pooling — Use
this.physics.add.group({ maxSize: 50 }) for bullets, particles, enemies; recycle instead of create/destroy
- Pixel art — Set
pixelArt: true and roundPixels: true in config; prevents blurry scaling on retina displays
- Mobile input — Add virtual joystick for touch; Phaser has built-in pointer events for multi-touch
- Texture atlases — Pack sprites into atlases (TexturePacker); reduces draw calls from 200+ to 5-10
- Camera — Use
startFollow with lerp values (0.05-0.1) for smooth camera; set bounds to tilemap size
1---2name: phaser3description: >-4 You are an expert in Phaser, the fast and feature-rich HTML5 game framework5 for making 2D games that run in web browsers and mobile devices. You help6 developers build arcade games, puzzle games, RPGs, platformers, and7 roguelikes using Phaser's scene system, physics engines (Arcade and8 Matter.js), sprite animations, tilemaps, tweens, particle effects, and input9 handling — with TypeScript support and Vite for modern development workflow.10license: Apache-2.011compatibility: ''12metadata:13 source: https://skillmd.com/skills/eliferjunior/phaser14 author: terminal-skills15 version: 1.0.016 category: Game Development17 tags:18 - game-engine19 - html520 - browser-game21 - 2d22 - arcade-physics23 - webgl24 - canvas25---2627# Phaser — HTML5 Game Framework for Browser Games2829You are an expert in Phaser, the fast and feature-rich HTML5 game framework for making 2D games that run in web browsers and mobile devices. You help developers build arcade games, puzzle games, RPGs, platformers, and roguelikes using Phaser's scene system, physics engines (Arcade and Matter.js), sprite animations, tilemaps, tweens, particle effects, and input handling — with TypeScript support and Vite for modern development workflow.3031## Core Capabilities3233### Game Configuration3435```typescript36// src/main.ts — Phaser game entry point37import Phaser from "phaser";38import { PreloadScene } from "./scenes/PreloadScene";39import { GameScene } from "./scenes/GameScene";40import { HUDScene } from "./scenes/HUDScene";4142const config: Phaser.Types.Core.GameConfig = {43 type: Phaser.AUTO, // WebGL → Canvas fallback44 width: 800,45 height: 600,46 pixelArt: true, // No anti-aliasing on sprites47 roundPixels: true, // Snap to pixel grid48 scale: {49 mode: Phaser.Scale.FIT, // Fit viewport, keep aspect ratio50 autoCenter: Phaser.Scale.CENTER_BOTH,51 },52 physics: {53 default: "arcade", // Fast AABB physics54 arcade: {55 gravity: { x: 0, y: 300 }, // Platformer gravity56 debug: false,57 },58 },59 scene: [PreloadScene, GameScene, HUDScene],60};6162new Phaser.Game(config);63```6465### Scene System6667```typescript68// src/scenes/GameScene.ts — Main game loop69export class GameScene extends Phaser.Scene {70 private player!: Phaser.Physics.Arcade.Sprite;71 private platforms!: Phaser.Physics.Arcade.StaticGroup;72 private coins!: Phaser.Physics.Arcade.Group;73 private score: number = 0;7475 constructor() {76 super("GameScene");77 }7879 create() {80 // Tilemap from Tiled editor81 const map = this.make.tilemap({ key: "level-1" });82 const tileset = map.addTilesetImage("terrain", "terrain-tiles")!;83 const ground = map.createLayer("Ground", tileset)!;84 ground.setCollisionByProperty({ collides: true });8586 // Player with animations87 this.player = this.physics.add.sprite(100, 200, "hero");88 this.player.setCollideWorldBounds(true);89 this.player.setBounce(0.1);9091 this.anims.create({92 key: "run",93 frames: this.anims.generateFrameNumbers("hero", { start: 0, end: 5 }),94 frameRate: 10,95 repeat: -1, // Loop forever96 });9798 // Collisions99 this.physics.add.collider(this.player, ground);100101 // Coins from object layer in Tiled102 const coinObjects = map.getObjectLayer("Coins")!.objects;103 this.coins = this.physics.add.group();104 coinObjects.forEach((obj) => {105 const coin = this.coins.create(obj.x!, obj.y!, "coin");106 coin.setScale(0.5);107 coin.body.setAllowGravity(false);108 });109110 this.physics.add.overlap(this.player, this.coins, this.collectCoin, undefined, this);111112 // Camera113 this.cameras.main.startFollow(this.player, true, 0.08, 0.08);114 this.cameras.main.setBounds(0, 0, map.widthInPixels, map.heightInPixels);115116 // Launch HUD as parallel scene117 this.scene.launch("HUDScene");118 }119120 update() {121 const cursors = this.input.keyboard!.createCursorKeys();122123 if (cursors.left.isDown) {124 this.player.setVelocityX(-160);125 this.player.anims.play("run", true);126 this.player.setFlipX(true);127 } else if (cursors.right.isDown) {128 this.player.setVelocityX(160);129 this.player.anims.play("run", true);130 this.player.setFlipX(false);131 } else {132 this.player.setVelocityX(0);133 this.player.anims.play("idle", true);134 }135136 // Jump (only when touching ground)137 if (cursors.up.isDown && this.player.body!.blocked.down) {138 this.player.setVelocityY(-330);139 }140 }141142 private collectCoin(143 _player: Phaser.GameObjects.GameObject,144 coin: Phaser.GameObjects.GameObject,145 ) {146 (coin as Phaser.Physics.Arcade.Sprite).disableBody(true, true);147 this.score += 10;148 this.events.emit("score-changed", this.score);149150 // Particle burst effect151 const particles = this.add.particles(coin.body!.position.x, coin.body!.position.y, "sparkle", {152 speed: 100,153 lifespan: 300,154 quantity: 8,155 scale: { start: 0.5, end: 0 },156 emitting: false,157 });158 particles.explode();159 }160}161```162163### Physics, Tweens, and Effects164165```typescript166// Arcade physics — fast, axis-aligned167this.physics.add.collider(player, enemies, onHit);168this.physics.add.overlap(bullet, enemies, onBulletHit);169player.setVelocity(200, -300);170player.setBounce(0.2);171player.setDrag(100);172173// Matter.js physics — complex shapes, joints, sensors174const ball = this.matter.add.circle(400, 200, 20, { restitution: 0.8 });175const constraint = this.matter.add.constraint(anchor, ball, 100, 0.1);176177// Tweens — smooth animations178this.tweens.add({179 targets: sprite,180 y: sprite.y - 50,181 alpha: 0,182 duration: 500,183 ease: "Power2",184 onComplete: () => sprite.destroy(),185});186187// Screen shake188this.cameras.main.shake(200, 0.01);189190// Time events191this.time.addEvent({192 delay: 2000,193 callback: spawnEnemy,194 loop: true,195});196```197198## Installation199200```bash201# New project with Vite202npm create vite@latest my-game -- --template vanilla-ts203cd my-game204npm install phaser205npm run dev206```207208## Best Practices2092101. **Scene system** — Use separate scenes for menu, game, HUD, pause, game-over; HUD as parallel scene overlay2112. **Arcade physics for simple games** — AABB collision is fast and sufficient for most 2D games; Matter.js only when you need complex shapes2123. **Tilemap integration** — Design levels in Tiled, export as JSON, load with `this.make.tilemap`; use object layers for spawn points2134. **Object pooling** — Use `this.physics.add.group({ maxSize: 50 })` for bullets, particles, enemies; recycle instead of create/destroy2145. **Pixel art** — Set `pixelArt: true` and `roundPixels: true` in config; prevents blurry scaling on retina displays2156. **Mobile input** — Add virtual joystick for touch; Phaser has built-in pointer events for multi-touch2167. **Texture atlases** — Pack sprites into atlases (TexturePacker); reduces draw calls from 200+ to 5-102178. **Camera** — Use `startFollow` with lerp values (0.05-0.1) for smooth camera; set bounds to tilemap size