Phaser HTML5 Game Framework
Phaser is a robust, open-source HTML5 game framework designed for creating 2D games across desktop and mobile web browsers, currently stable at version 4.0.0. It leverages WebGL and Canvas rendering and has been under active development for over a decade. Version 4 represents a significant, ground-up rebuild of its WebGL renderer, featuring a new, node-based architecture that replaces the v3 pipeline system for enhanced performance and reliability, while striving to maintain a familiar API for developers. The framework offers an extensive suite of features for 2D game development, including advanced physics engines, sophisticated tweening systems, and web audio management. It supports development using both JavaScript and TypeScript and is compatible with various modern front-end frameworks. The project is commercially developed and maintained by Phaser Studio Inc. with strong community contributions, adhering to a release cadence that includes extensive pre-release candidates for major versions, ensuring a stable and well-tested core.
Common errors
-
Uncaught ReferenceError: Phaser is not defined
cause The global `Phaser` object is not accessible, either because the script tag for Phaser was not loaded correctly (if using a CDN) or an `import` statement is missing or incorrect in a module environment.fixIf using a CDN, ensure the `<script>` tag for `phaser.min.js` is correctly placed in your HTML and loaded before your game code. If using modules, ensure `import Phaser from 'phaser';` is present at the top of your module file. -
TypeError: Cannot read properties of undefined (reading 'Scene')
cause Attempting to destructure or directly import a core Phaser class (like `Scene`, `Game`, `GameObjects`) that is not a named export from the `phaser` package. These classes are properties of the default `Phaser` object.fixAccess core classes and namespaces as properties of the default `Phaser` import: `class MyScene extends Phaser.Scene { ... }` or `const game = new Phaser.Game(...)`. -
Module not found: Error: Can't resolve 'phaser' in '[your_project_path]' (webpack)
cause The `phaser` package is not correctly installed or your build tool (e.g., Webpack, Vite, Rollup) cannot locate it, possibly due to an incorrect `node_modules` path or improper ES Module configuration.fixRun `npm install phaser` or `yarn add phaser` to ensure the package is installed. Verify your `tsconfig.json` (for TypeScript) or build tool configuration correctly handles module resolution for ES Modules. If migrating, ensure all `require()` calls are updated to `import` statements. -
WebGL error 1282: Invalid operation
-
Shader program compilation failed (WebGL)
cause These are common WebGL context errors, often indicating issues with custom shader uniform setup, incorrect texture binding, or invalid WebGL state changes. The new v4 renderer architecture is more strict with WebGL state management.fixCarefully review any custom shaders and direct WebGL interactions. Ensure shader uniform types and values match expectations. Refer to the Phaser v4 documentation on the new Render Node Architecture and `Shader#setUniform()` for correct usage and state management.
Warnings
- breaking Phaser v4 introduces a completely rewritten WebGL renderer with a new, node-based architecture, fundamentally changing the rendering pipeline from v3. The v3 pipeline system has been replaced.
- gotcha The full `phaser.js` bundle is over 8MB raw due to extensive inline JSDoc documentation, which is stripped during minification. Shipping this bundle to production will significantly increase load times.
- gotcha Texture wrapping modes like `REPEAT` may not apply correctly to non-power-of-two (NPOT) textures in WebGL, potentially causing unexpected visual artifacts.
- gotcha Phaser 4's module structure and official examples primarily leverage ES Modules (`import`). While some transpiled environments might accommodate CommonJS (`require`), it is not the recommended or fully supported approach, and may lead to module resolution issues.
- gotcha Significant performance optimizations related to data buffer sizing, especially for filters and masks, were introduced in v4.0.0-rc.4. While generally leading to speedups (up to 16x on mobile), specific, complex filter configurations might behave differently.
Install
-
npm install phaser -
yarn add phaser -
pnpm add phaser
Imports
- Phaser
const Phaser = require('phaser');import Phaser from 'phaser';
- Phaser.Scene (class access)
import { Scene } from 'phaser';import Phaser from 'phaser'; class MyScene extends Phaser.Scene { /* ... */ } - Phaser (Type Import)
import { Phaser } from 'phaser';import type Phaser from 'phaser';
Quickstart
import Phaser from 'phaser';
class DemoScene extends Phaser.Scene {
constructor() {
super({ key: 'DemoScene' });
}
preload() {
// Load assets here if needed, e.g., this.load.image('logo', 'assets/phaser3-logo.png');
}
create() {
this.add.text(100, 100, 'Hello Phaser 4!', { font: '32px Arial', color: '#ffffff' });
console.log('Phaser 4 Demo Scene created!');
// Example: Add a simple tween
const circle = this.add.circle(400, 300, 50, 0xff0000);
this.tweens.add({
targets: circle,
x: 600,
duration: 2000,
ease: 'Power2',
yoyo: true,
loop: -1
});
}
update(time: number, delta: number) {
// Game logic updated every frame
}
}
const config: Phaser.Types.Core.GameConfig = {
type: Phaser.AUTO,
width: 800,
height: 600,
backgroundColor: '#1a1a1a',
scene: DemoScene,
parent: 'game-container', // Ensure you have a div with id="game-container" in your HTML
physics: {
default: 'arcade',
arcade: {
debug: false
}
}
};
new Phaser.Game(config);