melonjs-getting-started
它能做什么
它介绍如何启动 melonJS 20 游戏,包括必需的设置步骤,以及如何添加第一个对象。适用于使用 melonJS 开始新浏览器游戏的开发者。
适用场景
- ✓当开发者开始一个新的 melonJS 游戏,需要第一个可正常运行的画面时。
- ✓当开发者已添加对象,但对象从未出现在屏幕上时。
- ✓当开发者需要了解游戏将使用哪种渲染器时。
技能链接
https://github.com/melonjs/melonJS/blob/master/packages/melonjs/skills/melonjs-getting-started/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请帮我设置一个基础的 melonJS 游戏,在屏幕中央显示居中的标题文字。”
每周 5 个新技能,1 分钟读完。
工作原理
该技能展示设置顺序:创建应用,await 其初始化,然后将对象添加到世界容器中。它还说明了渲染器的回退机制。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Getting started with melonJS
Bootstrapping a melonJS 20.x game: the
Applicationlifecycle, the mandatoryawait 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.
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:
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.
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.
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:
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.
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
常见问题
为什么我的屏幕上什么也没有绘制?
在 20 版本中,必须先用 await 初始化应用,才能添加任何内容,否则对象永远不会绘制。
什么是世界容器?
它是添加游戏对象的地方,这样对象才会出现在游戏中。
什么是渲染器回退?
游戏在可用时使用 WebGPU 或 WebGL 2,否则回退到 Canvas,而 Canvas 缺少部分功能。
详情
作者的原始描述
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.
repo: melonjs/melonJS
热度
- GitHub 星标
- 6.4k
- 排名
- #14203
- 许可证
- MIT
- 更新时间
- 2026/11/2
评论
- …
同一仓库的更多技能
melonjs-20-migration
6.4k帮你的智能体按 melonJS 20 版本编写游戏代码,避免使用旧版本中已过时的方法,例如必需的异步启动。
melonjs-3d-assets
6.4k帮你的智能体将 glTF、GLB 和 OBJ 等 3D 模型加载到 melonJS 浏览器游戏中,并提供缩放和光照选项。
melonjs-3d
6.4k帮你的智能体在 melonJS 浏览器游戏中构建 3D 或 2.5D 场景,涵盖相机、网格、灯光和深度,并遵循引擎自身的坐标轴规则。
melonjs-audio
6.4k帮你的智能体为 melonJS 游戏添加音效和音乐,包括音频设置、背景音轨、音量控制和空间声音。