Skip to main content

Build

Melonjs Getting Started

melonJS

Use this skill when starting or bootstrapping a melonJS game, creating the Application, choosing a renderer, or setting up the first scene. Covers the mandatory async init, the world container, adding renderables, and the WebGPU/WebGL 2/Canvas fallback. Triggers on: melonjs, new Application, app.init, app.world, video.AUTO, video.WEBGL, video.CANVAS, Stage, state.change, getting started, bootstrap.

  • New
  • v1.0.2
  • No reviews yet

Capabilities

  • The bootstrap

    Application construction is synchronous but initialisation is not

  • Top-level await

    The bootstrap needs await at module scope

  • Choosing a renderer

    Pin the renderer explicitly (video.WEBGL) only when you have a reason…

  • The scene graph

    Pass the z-order as the second argument to addChild — it is the only…

Getting started

Full instructions

Install the skill, then ask your AI tool for the task in plain words. The full SKILL.md below shows what it covers.

Instructions: SKILL.mdv1.0.2

Getting started with melonJS

Bootstrapping a melonJS 20.x game: the Application lifecycle, the mandatory
await app.init(), the world container, and what the renderer fallback means
for feature availability.

Applies to: melonJS 20.x. Older major versions differ — see the
melonjs-20-migration skill.

The bootstrap

Application construction is synchronous but initialisation is not. init()
returns a promise and must be awaited before building any renderable, loading
assets, or entering a scene.

js
import { Application, Text } from "melonjs";

const app = new Application(1218, 562, {
    parent: "screen",      // id of the DOM element to mount into
    scale: "auto",
    backgroundColor: "#202020",
});
await app.init();          // ← REQUIRED. Nothing below works without it.

app.world.addChild(
    new Text(609, 281, {
        font: "Arial",
        size: 160,
        fillStyle: "#FFFFFF",
        textAlign: "center",
        textBaseline: "middle",
        text: "Hello World !",
    }),
);

This is the single most common thing to get wrong. On 19.x the constructor
called init(width, height, options) for you; on 20.x init() takes no
arguments and calling — and awaiting — it is your job. Nothing that follows
names the missing call. The constructor does build app.world, so adding a
plain renderable succeeds and simply never draws — no canvas is appended and the
app never subscribes to the frame tick — while constructing a Sprite or a
Text throws Cannot read properties of undefined (reading 'renderer'),
because the global game those two fall back on is only set once the first
init() resolves. Either way app.renderer and app.viewport stay undefined
and the page stays blank.

init() is asynchronous because acquiring a WebGPU device is asynchronous. There
is no synchronous alternative.

Top-level await

The bootstrap needs await at module scope. In an ES module that is fine. If the
surrounding tooling does not allow it, wrap it:

js
async function main() {
    const app = new Application(800, 600, { parent: "screen" });
    await app.init();
    // …
}
void main();

Choosing a renderer

video.AUTO (the default) tries WebGPU → WebGL 2 → Canvas and uses the first
that works.

js
import { video } from "melonjs";

const app = new Application(800, 600, {
    parent: "screen",
    renderer: video.AUTO,      // default; or video.WEBGPU / video.WEBGL / video.CANVAS
});

Two consequences worth knowing before writing rendering code:

  • Which backend you get is a runtime fact, not a build-time one. The same
    code runs on a different renderer depending on the machine. Do not assume
    WebGL.
  • Not every feature exists on every backend. Shader effects and post effects
    need a programmable pipeline and are inert on Canvas. Where a feature degrades,
    the engine warns once rather than throwing — so a missing effect is a console
    warning, not a crash.

Pin the renderer explicitly (video.WEBGL) only when you have a reason to. It
costs you the WebGPU path and the Canvas fallback.

The scene graph

app.world is the root Container. Everything drawn is a descendant of it.

js
app.world.addChild(sprite, 10);   // 10 is the z / draw order

Pass the z-order as the second argument to addChild — it is the only form
that works. renderable.z does not exist as a property, setting depth before
addChild is overwritten by autoDepth, and setting it afterwards does not
resort. See the melonjs-renderables skill for the full explanation.

Scenes

For anything beyond a single screen, use Stage subclasses and the state
manager rather than mutating app.world directly:

js
import { Stage, state } from "melonjs";

class PlayScreen extends Stage {
    onResetEvent() { /* build the scene */ }
    onDestroyEvent() { /* tear it down */ }
}

state.set(state.PLAY, new PlayScreen());
state.change(state.PLAY);

onResetEvent runs every time the state is entered, not once — build scene
content there, not in the constructor.

Loading assets

Assets are declared and preloaded through loader; audio needs audio.init()
with the formats you ship before any audio asset is preloaded.

js
import { audio, loader } from "melonjs";

audio.init("mp3,ogg");                             // BEFORE preloading audio
await loader.preload([
    { name: "tileset", type: "image", src: "data/img/tileset.png" },
    { name: "jump",    type: "audio", src: "data/sfx/" },   // directory, not file
]);

A flat list suits a single-screen game. A game with several levels exports a
record of bundles instead — { common: [...], level1: [...], level2: [...] },
passed to the same loader.preload() — so each level's assets can later be
freed and loaded again by id; see melonjs-loading-assets.

Two ordering rules that produce confusing failures when missed:

  • audio.init() must run before any audio asset is preloaded, and names the
    formats you actually ship. Preloading first throws "target audio extension(s)
    should be set through me.audio.init() before calling the preloader"
    .
  • The audio src is a directory, not a file. The loader appends the asset
    name plus each configured extension, so a full filename gives you a 404.

anchorPoint defaults to the centre

Renderable.anchorPoint is (0.5, 0.5), so pos is an object's centre, not
its top-left. Container forces (0, 0) instead.

Writing top-left math against a centred anchor puts everything half a sprite off
with no error — it is the most common source of "close but subtly wrong"
placement.

Common mistakes

symptom cause
blank page, nothing drawn, or a TypeError reading renderer from new Sprite / new Text missing await app.init()
objects draw in the wrong order .z/depth set instead of addChild(child, z) — .z is not even a real property
shader/post effect does nothing, warning in console running on the Canvas renderer
audio 404s audio asset src given as a file rather than a directory
scene content appears once and never resets scene built in the constructor instead of onResetEvent
everything offset by half a sprite top-left math against the centred anchorPoint default
a Tween or ParticleEmitter does nothing .start() / streamParticles() never called
an asset on another origin fails to load, the error naming it that server sends no Access-Control-Allow-Origin header (see melonjs-loading-assets)
  • melonjs-20-migration — what changed in 20.x that older code and older
    training data get wrong
  • melonjs-renderables — custom Renderable subclasses, pointer events, post
    effects and the traps in each

Explore more skills

Browse all
This skill doesn’t include stateful context yet, instructions only. Learn about stateful skills.

Security

Loading security scan...

Reviews (0)

More from melonJS

View source →
melonjs-renderablesUse this skill when subclassing Renderable, writing a custom draw() method, handling pointer or click events on game objects, applying post effects and shaders, or controlling draw order. Covers the pos-relative draw contract, isKinematic…0 installsmelonjs-testingUse this skill to check that a melonJS game actually works before saying it does: running it in a headless browser, reading the console the engine writes, finding out which renderer the run got, driving the keyboard and the pointer,…0 installsmelonjs-ui-and-textUse this skill for HUDs, buttons, menus, options screens, dialogue panels, progress bars and on-screen text in melonJS. Covers UIBaseElement/UISpriteElement/UITextButton, UICheckbox, UISlider, ProgressBar, UIPanel (skins, layout,…0 installsmelonjs-inputUse this skill for keyboard, pointer, mouse, touch and gamepad input in melonJS. Covers the bindKey action indirection, registerPointerEvent on regions, the isKinematic requirement that silently blocks pointer events, which of several…0 installsmelonjs-performanceUse this skill when a melonJS game drops frames, allocates heavily, or needs to scale to many objects. Covers object pooling, draw-call batching, atlases, baking with CanvasRenderTarget, instancing, culling and alwaysUpdate, the debug…0 installsmelonjs-deploymentUse this skill when packaging, shipping or scaffolding a melonJS game — starting a project with create-melonjs, building for the web, browser support and transpiling, and bundle size. For running inside a game portal, a native app, a…0 installs

Frequently asked questions about melonjs-getting-started

What does the melonjs-getting-started skill do?

Use this skill when starting or bootstrapping a melonJS game, creating the Application, choosing a renderer, or setting up the first scene. Covers the mandatory async init, the world container, adding renderables, and the WebGPU/WebGL 2/Canvas fallback. Triggers on: melonjs, new Application, app.init, app.world, video.AUTO, video.WEBGL, video.CANVAS, Stage, state.change, getting started, bootstrap. It's a reusable SKILL.md instruction set that loads into your AI coding assistant on demand, no prompt engineering, no copy-pasting every session.

How do I install the melonjs-getting-started skill?

Run `npx @skills-hub-ai/cli install melonjs-getting-started` from your terminal. The CLI writes the SKILL.md to the correct location for your AI tool (e.g. ~/.claude/skills/melonjs-getting-started/ for Claude Code or ~/.cursor/skills/ for Cursor with --target cursor) and adds it to your project's .skills.json lockfile.

Which AI tools does melonjs-getting-started work with?

melonjs-getting-started runs in Claude Code. It follows the open Agent Skills standard (SKILL.md), so the same skill works in every supported tool without modification.

Is the melonjs-getting-started skill free?

Yes. Every skill on skills-hub.ai is free and open-source. There are no premium tiers, paywalls, or usage limits. You only pay for whatever AI assistant you're already using.

How do I use melonjs-getting-started after installing it?

In Claude Code, type `/melonjs-getting-started` (or whatever slash command the skill registers) and the AI follows the skill's instructions immediately. You can also reference it by name in natural language, your AI loads the skill into context when relevant.

Can I share the melonjs-getting-started skill with my team?

Yes. Commit your project's .skills.json lockfile and teammates run `npx @skills-hub-ai/cli install` (no args) to install every skill at the exact version you pinned. Organization-scoped installs work via skills-hub.ai organizations.

Badge for your README

Show that this skill is on Skills Hub. The badge links back to this page and shows the live install count.

Install on Skills Hub badge preview
Markdown
[![Install on Skills Hub](https://skills-hub.ai/api/badges/skill/melonjs-getting-started)](https://skills-hub.ai/skills/melonjs-getting-started)
HTML
<a href="https://skills-hub.ai/skills/melonjs-getting-started"><img src="https://skills-hub.ai/api/badges/skill/melonjs-getting-started" alt="Install on Skills Hub"></a>

More options in the badge docs.