Excalibur.js — TypeScript-First 2D Game Engine
You are an expert in Excalibur.js, the TypeScript-first 2D game engine built for the web. You help developers build browser games using Excalibur's Actor system, Scene management, Tiled integration, physics, animation, sound, and input handling — with first-class TypeScript support, excellent documentation, and a focus on developer experience over raw performance.
Core Capabilities
Game Setup
// src/main.ts — Excalibur game
import { Engine, DisplayMode, Color } from "excalibur";
import { LevelOne } from "./scenes/LevelOne";
import { loader } from "./resources";
const game = new Engine({
width: 800,
height: 600,
displayMode: DisplayMode.FitScreen,
backgroundColor: Color.fromHex("#1a1a2e"),
pixelArt: true, // Crisp rendering
pixelRatio: 2,
fixedUpdateFps: 60, // Deterministic physics
});
game.addScene("level-one", new LevelOne());
game.start(loader).then(() => { // Preload assets
game.goToScene("level-one");
});
Actors and Components
// src/actors/Player.ts
import { Actor, Color, vec, Keys, CollisionType, Animation, SpriteSheet } from "excalibur";
import { Resources } from "../resources";
export class Player extends Actor {
private speed = 200;
private jumpForce = -400;
private health = 3;
private isGrounded = false;
constructor(x: number, y: number) {
super({
pos: vec(x, y),
width: 16,
height: 24,
collisionType: CollisionType.Active, // Moves and collides
color: Color.Green,
});
}
onInitialize(engine: Engine) {
// Sprite sheet animations
const spriteSheet = SpriteSheet.fromImageSource({
image: Resources.HeroSheet,
grid: { rows: 4, columns: 6, spriteWidth: 16, spriteHeight: 24 },
});
const idle = Animation.fromSpriteSheet(spriteSheet, [0, 1, 2, 3], 200);
const run = Animation.fromSpriteSheet(spriteSheet, [6, 7, 8, 9, 10, 11], 100);
const jump = Animation.fromSpriteSheet(spriteSheet, [12, 13], 150);
this.graphics.add("idle", idle);
this.graphics.add("run", run);
this.graphics.add("jump", jump);
this.graphics.use("idle");
// Ground detection
this.on("postcollision", (evt) => {
if (evt.side === "Bottom") this.isGrounded = true;
});
}
onPreUpdate(engine: Engine, delta: number) {
const kb = engine.input.keyboard;
let moving = false;
if (kb.isHeld(Keys.ArrowLeft)) {
this.vel.x = -this.speed;
this.graphics.flipHorizontal = true;
moving = true;
} else if (kb.isHeld(Keys.ArrowRight)) {
this.vel.x = this.speed;
this.graphics.flipHorizontal = false;
moving = true;
} else {
this.vel.x = 0;
}
if (kb.wasPressed(Keys.Space) && this.isGrounded) {
this.vel.y = this.jumpForce;
this.isGrounded = false;
this.graphics.use("jump");
} else if (moving) {
this.graphics.use("run");
} else {
this.graphics.use("idle");
}
}
takeDamage(amount: number) {
this.health -= amount;
// Flash red
this.actions.blink(100, 100, 5);
if (this.health <= 0) {
this.scene?.engine.goToScene("game-over");
}
}
}
Scenes and Tiled Maps
// src/scenes/LevelOne.ts
import { Scene, Engine, TileMap, vec } from "excalibur";
import { TiledResource } from "@excaliburjs/plugin-tiled";
import { Player } from "../actors/Player";
import { Coin } from "../actors/Coin";
export class LevelOne extends Scene {
private tiledMap!: TiledResource;
onInitialize(engine: Engine) {
this.tiledMap = new TiledResource("/maps/level-1.tmx");
// Add tilemap to scene
this.tiledMap.addToScene(this);
// Get spawn point from Tiled object layer
const spawnPoint = this.tiledMap.getObjectsByName("PlayerSpawn")[0];
const player = new Player(spawnPoint.x, spawnPoint.y);
this.add(player);
// Camera follows player
this.camera.strategy.elasticToActor(player, 0.8, 0.9);
this.camera.zoom = 2;
// Spawn coins from object layer
this.tiledMap.getObjectsByType("coin").forEach((obj) => {
this.add(new Coin(obj.x, obj.y));
});
}
}
Installation
npm install excalibur
npm install @excaliburjs/plugin-tiled # Tiled map support
Best Practices
- TypeScript always — Excalibur is built in TypeScript; use it for full autocompletion and type safety
- Actor lifecycle — Override
onInitialize, onPreUpdate, onPostUpdate instead of constructor for game logic
- Collision types — Use
Active for moving entities, Fixed for static platforms, Passive for triggers/sensors
- Scene transitions —
engine.goToScene("name", { sceneActivationData }) to pass data between scenes
- Tiled plugin — Use the official Tiled plugin for level design; supports tile layers, object layers, and custom properties
- Actions API — Chain animations:
actor.actions.moveTo(100, 100, 200).delay(500).fade(0, 1000) for cutscenes and effects
- Event system — Use typed events (
on("precollision"), on("kill")) for clean game logic
- Resource loading — Define all assets in a loader; Excalibur shows a loading screen automatically
1---2name: excalibur3description: >-4 You are an expert in Excalibur.js, the TypeScript-first 2D game engine5 built for the web. You help developers build browser games using Excalibur's6 Actor system, Scene management, Tiled integration, physics, animation,7 sound, and input handling — with first-class TypeScript support, excellent8 documentation, and a focus on developer experience over raw performance.9license: Apache-2.010compatibility: ''11metadata:12 source: https://skillmd.com/skills/eliferjunior/excalibur13 author: terminal-skills14 version: 1.0.015 category: Game Development16 tags:17 - game-engine18 - typescript19 - 2d20 - ecs21 - browser-game22 - webgl23---2425# Excalibur.js — TypeScript-First 2D Game Engine2627You are an expert in Excalibur.js, the TypeScript-first 2D game engine built for the web. You help developers build browser games using Excalibur's Actor system, Scene management, Tiled integration, physics, animation, sound, and input handling — with first-class TypeScript support, excellent documentation, and a focus on developer experience over raw performance.2829## Core Capabilities3031### Game Setup3233```typescript34// src/main.ts — Excalibur game35import { Engine, DisplayMode, Color } from "excalibur";36import { LevelOne } from "./scenes/LevelOne";37import { loader } from "./resources";3839const game = new Engine({40 width: 800,41 height: 600,42 displayMode: DisplayMode.FitScreen,43 backgroundColor: Color.fromHex("#1a1a2e"),44 pixelArt: true, // Crisp rendering45 pixelRatio: 2,46 fixedUpdateFps: 60, // Deterministic physics47});4849game.addScene("level-one", new LevelOne());50game.start(loader).then(() => { // Preload assets51 game.goToScene("level-one");52});53```5455### Actors and Components5657```typescript58// src/actors/Player.ts59import { Actor, Color, vec, Keys, CollisionType, Animation, SpriteSheet } from "excalibur";60import { Resources } from "../resources";6162export class Player extends Actor {63 private speed = 200;64 private jumpForce = -400;65 private health = 3;66 private isGrounded = false;6768 constructor(x: number, y: number) {69 super({70 pos: vec(x, y),71 width: 16,72 height: 24,73 collisionType: CollisionType.Active, // Moves and collides74 color: Color.Green,75 });76 }7778 onInitialize(engine: Engine) {79 // Sprite sheet animations80 const spriteSheet = SpriteSheet.fromImageSource({81 image: Resources.HeroSheet,82 grid: { rows: 4, columns: 6, spriteWidth: 16, spriteHeight: 24 },83 });8485 const idle = Animation.fromSpriteSheet(spriteSheet, [0, 1, 2, 3], 200);86 const run = Animation.fromSpriteSheet(spriteSheet, [6, 7, 8, 9, 10, 11], 100);87 const jump = Animation.fromSpriteSheet(spriteSheet, [12, 13], 150);8889 this.graphics.add("idle", idle);90 this.graphics.add("run", run);91 this.graphics.add("jump", jump);92 this.graphics.use("idle");9394 // Ground detection95 this.on("postcollision", (evt) => {96 if (evt.side === "Bottom") this.isGrounded = true;97 });98 }99100 onPreUpdate(engine: Engine, delta: number) {101 const kb = engine.input.keyboard;102 let moving = false;103104 if (kb.isHeld(Keys.ArrowLeft)) {105 this.vel.x = -this.speed;106 this.graphics.flipHorizontal = true;107 moving = true;108 } else if (kb.isHeld(Keys.ArrowRight)) {109 this.vel.x = this.speed;110 this.graphics.flipHorizontal = false;111 moving = true;112 } else {113 this.vel.x = 0;114 }115116 if (kb.wasPressed(Keys.Space) && this.isGrounded) {117 this.vel.y = this.jumpForce;118 this.isGrounded = false;119 this.graphics.use("jump");120 } else if (moving) {121 this.graphics.use("run");122 } else {123 this.graphics.use("idle");124 }125 }126127 takeDamage(amount: number) {128 this.health -= amount;129 // Flash red130 this.actions.blink(100, 100, 5);131 if (this.health <= 0) {132 this.scene?.engine.goToScene("game-over");133 }134 }135}136```137138### Scenes and Tiled Maps139140```typescript141// src/scenes/LevelOne.ts142import { Scene, Engine, TileMap, vec } from "excalibur";143import { TiledResource } from "@excaliburjs/plugin-tiled";144import { Player } from "../actors/Player";145import { Coin } from "../actors/Coin";146147export class LevelOne extends Scene {148 private tiledMap!: TiledResource;149150 onInitialize(engine: Engine) {151 this.tiledMap = new TiledResource("/maps/level-1.tmx");152153 // Add tilemap to scene154 this.tiledMap.addToScene(this);155156 // Get spawn point from Tiled object layer157 const spawnPoint = this.tiledMap.getObjectsByName("PlayerSpawn")[0];158 const player = new Player(spawnPoint.x, spawnPoint.y);159 this.add(player);160161 // Camera follows player162 this.camera.strategy.elasticToActor(player, 0.8, 0.9);163 this.camera.zoom = 2;164165 // Spawn coins from object layer166 this.tiledMap.getObjectsByType("coin").forEach((obj) => {167 this.add(new Coin(obj.x, obj.y));168 });169 }170}171```172173## Installation174175```bash176npm install excalibur177npm install @excaliburjs/plugin-tiled # Tiled map support178```179180## Best Practices1811821. **TypeScript always** — Excalibur is built in TypeScript; use it for full autocompletion and type safety1832. **Actor lifecycle** — Override `onInitialize`, `onPreUpdate`, `onPostUpdate` instead of constructor for game logic1843. **Collision types** — Use `Active` for moving entities, `Fixed` for static platforms, `Passive` for triggers/sensors1854. **Scene transitions** — `engine.goToScene("name", { sceneActivationData })` to pass data between scenes1865. **Tiled plugin** — Use the official Tiled plugin for level design; supports tile layers, object layers, and custom properties1876. **Actions API** — Chain animations: `actor.actions.moveTo(100, 100, 200).delay(500).fade(0, 1000)` for cutscenes and effects1887. **Event system** — Use typed events (`on("precollision")`, `on("kill")`) for clean game logic1898. **Resource loading** — Define all assets in a loader; Excalibur shows a loading screen automatically