跳到正文
Skillpedia
通用编程6.4k · #14194

melonjs-20-migration

它能做什么

它列出 melonJS 20 版本的变化,以及哪些旧函数已不再可用。适用于使用 melonJS 浏览器游戏引擎的游戏开发者。

适用场景

  • ✓当开发者要将 melonJS 游戏从 19 版或更早的版本升级时。
  • ✓当开发者编写 melonJS 代码,而预期的方法不存在时。
  • ✓当开发者启动 melonJS 游戏,但设置完成后屏幕一直空白时。

技能链接

https://github.com/melonjs/melonJS/blob/master/packages/melonjs/skills/melonjs-20-migration/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/melonjs/melonJS/blob/master/packages/melonjs/skills/melonjs-20-migration/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“我升级之后,melonJS 游戏显示空白屏幕。20 版本有哪些变化?”

每周 5 个新技能,1 分钟读完。

工作原理

该技能按可能性从高到低列出主要变化,例如应用设置时必须使用 await,并指出哪些旧的导出已被移除或重命名。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Migrating to melonJS 20.x

What changed in melonJS 20.x, and which older APIs are gone, deprecated, or renamed. Read this before writing melonJS code from memory — much of what is published about melonJS predates 20.0.

Why this skill exists: most melonJS material in circulation describes 19.x or earlier. Code written from that knowledge looks correct and fails at runtime. The items below are ordered by how likely they are to be reproduced from memory.

1. Application.init() is mandatory and asynchronous

Since 20.0, constructing an Application no longer initialises it.

// ✗ pre-20.x — the constructor called init(width, height, options) for you
const app = new Application(800, 600, { parent: "screen" });
app.world.addChild(sprite);          // added, but nothing ever renders

// ✓ 20.x
const app = new Application(800, 600, { parent: "screen" });
await app.init();                    // REQUIRED
app.world.addChild(sprite);

init() is async because acquiring a WebGPU device is async. There is no synchronous alternative, and no automatic fallback that papers over the missing call. Nothing that then breaks names it: the constructor still builds app.world, so children can be added and simply never draw — no canvas is appended, the app never subscribes to the frame tick, and app.renderer / app.viewport stay undefined — while constructing a Sprite or Text first throws a TypeError on the unset global game instead.

The signature changed too: 19.x took init(width, height, options), 20.x takes none, and anything passed is ignored (settings come from the constructor).

video.init(...) no longer exists. The video module now exports only the four renderer constants (AUTO, CANVAS, WEBGL, WEBGPU) — there is no init on it to call. Any code shaped like me.video.init(800, 600, {...}) is from a removed API. Build an Application and await init().

Note that import * as me from "melonjs" still works, so me.Sprite style is fine. It is the globals bootstrap that is gone, not the namespace import.

2. Post effects replaced the shader property

renderable.shader = … is deprecated since 19.2.0. Use the post-effect API:

// ✗ deprecated
mySprite.shader = new ShaderEffect(app.renderer, glslBody);

// ✓
mySprite.addPostEffect(new ShaderEffect(app.renderer, glslBody));

Related trap: removePostEffect() destroys the effect — and so does assigning renderable.shader, which destroys whatever it replaces. Only an effect carrying shared = true is spared. To turn one off temporarily, toggle effect.enabled instead: remove-and-re-add hands you back an object whose GPU resources have already been freed.

3. WebGL 2 is the baseline; WebGPU is the default attempt

video.AUTO now tries WebGPU → WebGL 2 → Canvas. WebGL 1 is gone.

Consequences:

  • Which backend runs is a runtime fact. Do not write code that assumes WebGL.
  • Custom shaders can carry both GLSL and WGSL in one asset, so one effect runs on either GPU backend.
  • Features needing a programmable pipeline are inert on Canvas and warn once rather than throwing.

4. Deprecated exports still present

These resolve but should not be used in new code:

deprecated since use instead
CanvasTexture 17.1.0 CanvasRenderTarget
Compositor 18.1.0 WebGLBatcher
PrimitiveCompositor 18.1.0 PrimitiveBatcher
QuadCompositor 18.1.0 QuadBatcher
Math (capitalised) 18.0.0 math (lowercase)
device.requestFullscreen / exitFullscreen 19.7.0 app.requestFullscreen() / app.exitFullscreen()
renderable.shader = … 19.2.0 addPostEffect()
Entity 18.1.0 Sprite / Renderable + a Body
loader.onload / onProgress / onError 18.2.0, removed 20.3 the LOADER_* events, or preload(assets, onloadcb)
response.overlap / overlapN / overlapV — response.depth / response.normal
setLineWidth() 17.3.0 the lineWidth property

Two of these deserve emphasis:

  • Entity is still exported, so code using it compiles and runs while being the wrong shape for 20.x. Compose a Sprite/Renderable with a Body instead.
  • The capitalised Math shadows the global Math if imported carelessly. me.Math.random() from older docs resolves to the deprecated re-export.

5. The global game is not the modern shape

game is still exported, but since 20.

常见问题

最常见的错误是什么?

该技能说明,升级后最常见的问题是漏掉了应用初始化时必需的 await。

为什么会出现空白屏幕?

在 20 版本中,创建应用并不会启动它,因此在初始化被 await 之前,什么都不会绘制。

它涵盖 WebGPU 选项吗?

涵盖。它说明了 WebGL 2 的基线以及 WebGPU 后端。

详情

作者的原始描述

Use this skill when writing melonJS code that may be based on pre-20.x knowledge, when migrating a game from melonJS 19.x or earlier, or when an API you expected does not exist. Covers the mandatory async Application.init(), the WebGL 2 baseline and WebGPU backend, renderable post effects replacing the shader property, and the deprecated exports still present. Triggers on: migration, upgrade, melonJS 19, me.game, me.video.init, renderable.shader, Compositor, CanvasTexture, me.Math, deprecated.

repo: melonjs/melonJS

热度

GitHub 星标
6.4k
排名
#14194
许可证
MIT
更新时间
2026/11/2

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能