学习路径Learning path · 第 2 阶段Stage 2
关卡数据的组织方式How to organise level data
How to organise level data
一句话:关卡应该是数据,不是代码。
(这是本站基于「数据与代码分离」工程常识的判断,下面给出主流引擎的官方做法作依据。)
In one line: levels should be data, not code.
(This is this site's engineering judgement about separating data from code; the mainstream engines' official practice below is the evidence.)
主流做法:网格(tilemap)+ 图层
三大引擎的官方文档都把关卡描述成「网格上的数据」:
| 引擎 | 官方说法(当日核对) | 出处 |
|---|---|---|
| Godot | 「tilemap 是用瓦片组成的网格,用来搭建游戏布局」;用 TileMapLayer 节点设计关卡 | 官方教程 Using TileMaps,2026-10-29 |
| Phaser | 官方入门教程「Making your first Phaser 3 game」用 tilemap 搭关卡 | phaser.io 官方教程 Part 5,2026-10-29 |
| Tiled(编辑器) | TMX 格式分 tile layer(地形)与 (出生点、机关等对象) | TMX map format 参考,2026-10-29 |
⚠ 这张表核对的是「官方文档怎么说」,不是「你必须用这一种」,三家一致说明这是被验证的默认路径。
The mainstream approach: a tile grid plus layers
All three engines' official docs describe levels as data on a grid:
| Engine | Official wording (checked that day) | Source |
|---|---|---|
| Godot | "a tilemap is a grid of tiles used to create a game's layout"; design levels with TileMapLayer nodes | official tutorial Using TileMaps, 2026-10-29 |
| Phaser | the official Making your first Phaser 3 game tutorial builds its level from a tilemap | phaser.io tutorial Part 5, 2026-10-29 |
| Tiled (editor) | the TMX format splits tile layersobject layers (spawns, triggers, etc.) | TMX map format reference, 2026-10-29 |
⚠ The table checks what the official docs say, not "you must use this one" — but three engines agreeing is a sign this is the validated default path.
为什么这对 AI 工作流特别重要
关卡在代码里时,「把第三段平台往左移」这种小改动意味着让 AI 重写一段渲染+碰撞代码:每次重写都是一次出错机会。关卡在数据里时,同样的改动只是改几个数字或一格瓦片,AI 出错一眼能看出来。
- 地形 → 网格数据(tilemap 文件,或代码里的二维数组)
- 出生点、机关、可互动物 → 对象层或单独的配置数组
- 碰撞 → 用引擎自带的碰撞层/碰撞组,不要让 AI 手写坐标区间
(这一节是本站对 AI 迭代方式的分析,不是引擎文档原话。)
Why this matters extra for AI workflows
When the level lives in code, "move the third platform left" means asking AI to rewrite a chunk of rendering plus collision code — every rewrite is a fresh chance to break something. When the level lives in data, the same change is a few numbers or one tile cell, and a mistake is obvious at a glance.
- Terrain → grid data (a tilemap file, or a 2D array in code)
- Spawns, traps, interactables → an object layer or a separate config array
- Collision → the engine's built-in collision layer/groups; never let AI hand-write coordinate ranges
(This section is this site's analysis of AI iteration, not engine-doc wording.)
什么时候不用 Tiled 也够
极简原型(见 最小可玩原型)里,代码内一个二维数组就是关卡数据:不需要编辑器、不需要 TMX 文件。等你需要可视化编辑、多关卡管理时再迁到 Tiled 这类工具,迁移成本很低(数据本来就在数据里)。
⚠ 反模式是把关卡「画」在代码里:每个平台一行 add.rectangle(x, y, w, h)。原型阶段偶尔可以,改三版之后它会变成最难维护的部分。
When you don't need Tiled
For a bare prototype (see minimal playable prototype), a 2D array in the code is level data — no editor, no TMX file. Move to a tool like Tiled when you want visual editing or multiple levels; the migration is cheap because the data was already data.
⚠ The anti-pattern is "drawing" the level in code: one add.rectangle(x, y, w, h) line per platform. Fine occasionally in a prototype; after three revisions it becomes the hardest part to maintain.
有序路径 · 第 6 / 26 篇Ordered path · item 6 of 26