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

melonjs-scenes-and-state

它能做什么

它介绍如何创建游戏画面,使用状态管理器在它们之间切换,并管理计时器和补间动画。适用于规划游戏流程的游戏开发者。

适用场景

  • ✓当开发者想要一个能进入游戏画面的菜单画面时。
  • ✓当开发者的场景内容在返回某个画面后没有重新构建时。
  • ✓当开发者想为游戏对象添加淡入淡出、脉冲或定时动画时。

技能链接

https://github.com/melonjs/melonJS/blob/master/packages/melonjs/skills/melonjs-scenes-and-state/SKILL.md

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

使用方法

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

可直接粘贴的消息

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

可以这样问 AI

“请在我的 melonJS 游戏中设置一个菜单画面和一个游戏画面,并加一个在两者之间切换的按钮。”

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

工作原理

该技能展示如何定义画面类,在重置处理函数中构建其内容,用状态 ID 注册它,然后切换到该画面。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Scenes, game state and timing

Stages and the state manager

A Stage is a screen. Register it against a state id, then change to it:

import { Stage, state } from "melonjs";

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

state.set(state.PLAY, new PlayScreen());   // register first
state.change(state.PLAY);                   // then switch

Built-in ids: state.LOADING, state.MENU, state.READY, state.PLAY, state.GAMEOVER (no underscore), state.GAME_END, state.SCORE, state.CREDITS, state.SETTINGS, state.DEFAULT. state.USER (100) is the base for your own ids — const CUTSCENE = state.USER + 1.

Build scene content in onResetEvent, not the constructor. onResetEvent runs on every entry to the state; the constructor runs once. Scene content created in the constructor exists once and is not rebuilt when you return.

onResetEvent(app) receives the Application — use that rather than importing the global game.

Extra arguments to state.change are forwarded:

state.change(state.PLAY, false, { level: 2 });   // → onResetEvent(app, { level: 2 })

The second argument is forceChange: pass true to switch immediately instead of on a deferred tick, which is what you want when changing state right after a preload completes. It is ignored when a transition is configured — a transition always drives the switch from its own completion callback.

onResetEvent runs when the switch is made. The first frame, STATE_CHANGE and a transition's reveal follow once the shaders the stage queued are ready, which is a few milliseconds once the browser has compiled them before, and at most 1.5s on a first visit. So build the stage in onResetEvent: an object a STATE_CHANGE listener adds is not waited for, and its first draw may still compile what it needs. That first frame steps the world once before drawing it, however short it is, so everything onResetEvent added is in it (the same goes for the frame after a level.load made while the game runs). Before 20.12 it was drawn unstepped, and showed the stage's background alone for one frame.

state.change throws Undefined Stage for state '<id>' if nothing was registered for that id, and state.set throws if the second argument is not a Stage instance. Neither fails quietly.

Always leave state.LOADING — it is transitional

loader.preload() switches to state.LOADING for you. Building your scene in the preload callback and then staying there looks like it works, and is one of the easiest mistakes to make:

// WRONG — the game now runs inside the loading stage, forever
loader.preload(assets, () => {
    app.world.addChild(new Player(...));
});

Nothing ever destroys that stage, so the built-in loading screen's logo and progress bar are never removed and sit on top of your game. (Eight of this repository's own examples did this before it was noticed, so it is not an obscure trap.)

Give the scene a stage of its own and switch to it:

class PlayScreen extends Stage {
    onResetEvent(app) {
        app.world.addChild(new Player(...));
    }
}

await loader.preload(assets);
state.set(state.PLAY, new PlayScreen());
state.change(state.PLAY);

The awaited form reads better here than the callback, because "load, then switch" is exactly what the code then says. If you genuinely want to keep using the loading stage as your scene, state.change(state.DEFAULT) at least gets you out of it — but a stage of your own is what you want.

Persistent objects across levels

if (typeof this.HUD === "undefined") {
    this.HUD = new Container();         // construct once (or UIBaseElement)
}
this.HUD.isPersistent = true;           // survives level changes
this.HUD.floating = true;               // screen coordinates, not world
app.world.addChild(this.HUD, 100);      // explicit z — see melonjs-renderables

Transitions and pausing

state.transition("fade", "#FFFFFF", 250);
state.transition("mask", "#000", 500, new Ellipse(0, 0, 1, 1));  // shape required
state.transition("pixelate", "#000", 300);  // blocks up to 32px; a 4th argument sets the size
state.pause();                               // freeze update, keep drawing
state.resume();
app.freeze(150);                             // brief hit-stop (proxies state.freeze)

state.transition is global and sticky — once set it applies to every later

常见问题

为什么返回某个画面后,场景内容消失了?

该技能说明,场景内容应放在重置处理函数中,每次进入该画面时都会运行它。

切换画面的顺序是什么?

该技能说明应先注册画面,然后再切换到它。

切换画面时可以传递数据吗?

可以。传给切换调用的额外参数会被转发给重置处理函数。

详情

作者的原始描述

Use this skill for scene structure and game flow in melonJS — Stage subclasses, the state manager, transitions, pausing, the update loop, timers and tweens. Covers onResetEvent versus the constructor, state.set before state.change, the camera and world lifecycle per stage, and pause-aware timing. Triggers on: Tween, easing, fade out, flash, pulse, punch, shrink back, ease in, ease out, animate over time, countdown in update, Stage, state, state.set, state.change, state.transition, state.pause, onResetEvent, onDestroyEvent, onActivateEvent, onDeactivateEvent, PLAY, MENU, LOADING, update loop, G

repo: melonjs/melonJS

热度

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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能