Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

docs: fix code example errors found during cross-page audit

esenginec4cb979a1d96
diff --git a/docs/astro/src/content/docs/core-concepts/systems.mdx b/docs/astro/src/content/docs/core-concepts/systems.mdx
index 25a054913..73f179b15 100644
--- a/docs/astro/src/content/docs/core-concepts/systems.mdx
+++ b/docs/astro/src/content/docs/core-concepts/systems.mdx
@@ -71,7 +71,9 @@ addSystemToSchedule(Schedule.FixedUpdate, physicsSystem);
Create, modify, and destroy entities at runtime:
```typescript
-import { Commands, Sprite, LocalTransform } from 'esengine';
+import { Commands, Sprite, LocalTransform, Velocity, defineResource } from 'esengine';
+
+const Score = defineResource({ value: 0 });
defineSystem([Commands()], (cmds) => {
// Spawn a new entity with components (chainable)
diff --git a/docs/astro/src/content/docs/guides/plugins.mdx b/docs/astro/src/content/docs/guides/plugins.mdx
index 66ff9faaa..75e6b2ac9 100644
--- a/docs/astro/src/content/docs/guides/plugins.mdx
+++ b/docs/astro/src/content/docs/guides/plugins.mdx
@@ -65,7 +65,7 @@ Use `defineSystem()` and `addSystem()` to add game logic that runs every frame:
```typescript
import { defineSystem, addSystem, Schedule } from 'esengine';
-addSystem(defineSystem([], (world) => {
+addSystem(defineSystem([], () => {
// runs every frame during the Update phase
}));
```
@@ -75,7 +75,7 @@ You can also target a specific schedule phase:
```typescript
import { addSystemToSchedule, defineSystem, Schedule } from 'esengine';
-addSystemToSchedule(Schedule.FixedUpdate, defineSystem([], (world) => {
+addSystemToSchedule(Schedule.FixedUpdate, defineSystem([], () => {
// runs at fixed timestep, ideal for physics logic
}));
```
@@ -85,7 +85,7 @@ For one-time initialization, use `addStartupSystem()`:
```typescript
import { addStartupSystem, defineSystem } from 'esengine';
-addStartupSystem(defineSystem([], (world) => {
+addStartupSystem(defineSystem([], () => {
// runs once when the game starts
}));
```
@@ -99,15 +99,15 @@ addStartupSystem(defineSystem([], (world) => {
Resources are global singleton data that can be shared across systems:
```typescript
-import { defineResource, addStartupSystem, defineSystem } from 'esengine';
+import { defineResource, addStartupSystem, defineSystem, Commands } from 'esengine';
const GameState = defineResource<{ score: number; level: number }>(
{ score: 0, level: 1 },
'GameState'
);
-addStartupSystem(defineSystem([], (world) => {
- world.app.insertResource(GameState, { score: 0, level: 1 });
+addStartupSystem(defineSystem([Commands()], (cmds) => {
+ cmds.insertResource(GameState, { score: 0, level: 1 });
}));
```
@@ -136,15 +136,15 @@ Systems run in a defined order each frame:
A plugin is an object implementing the `Plugin` interface:
```typescript
-import { type Plugin, type App, defineResource, defineSystem, Schedule, registerComponent } from 'esengine';
+import { type Plugin, type App, defineResource, defineComponent, defineSystem, Schedule } from 'esengine';
const ScoreData = defineResource<{ value: number }>({ value: 0 }, 'Score');
+const Health = defineComponent('Health', { hp: 100, maxHp: 100 });
const myPlugin: Plugin = {
name: 'MyGamePlugin',
build(app: App) {
app.insertResource(ScoreData, { value: 0 });
- registerComponent('Health', { hp: 100, maxHp: 100 });
app.addSystemToSchedule(Schedule.Update, defineSystem([], () => {
// game logic
}));
diff --git a/docs/astro/src/content/docs/guides/spine.mdx b/docs/astro/src/content/docs/guides/spine.mdx
index e32a0ef64..60d71eed5 100644
--- a/docs/astro/src/content/docs/guides/spine.mdx
+++ b/docs/astro/src/content/docs/guides/spine.mdx
@@ -110,7 +110,7 @@ const CharacterState = defineComponent('CharacterState', {
});
addSystem(defineSystem(
- [Res(Input), Res(Time), Query(Mut(LocalTransform), Mut(SpineAnimation), CharacterState)],
+ [Res(Input), Res(Time), Query(Mut(LocalTransform), Mut(SpineAnimation), Mut(CharacterState))],
(input, time, query) => {
for (const [entity, transform, spine, state] of query) {
let moving = false;
diff --git a/docs/astro/src/content/docs/guides/sprite.mdx b/docs/astro/src/content/docs/guides/sprite.mdx
index a1df27a39..841f1c395 100644
--- a/docs/astro/src/content/docs/guides/sprite.mdx
+++ b/docs/astro/src/content/docs/guides/sprite.mdx
@@ -102,7 +102,7 @@ const Animation = defineComponent('Animation', {
});
addSystem(defineSystem(
- [Res(Time), Query(Mut(Sprite), Animation)],
+ [Res(Time), Query(Mut(Sprite), Mut(Animation))],
(time, query) => {
for (const [entity, sprite, anim] of query) {
anim.elapsed += time.delta;
diff --git a/docs/astro/src/content/docs/guides/ui.mdx b/docs/astro/src/content/docs/guides/ui.mdx
index d8c04e411..a6183ef44 100644
--- a/docs/astro/src/content/docs/guides/ui.mdx
+++ b/docs/astro/src/content/docs/guides/ui.mdx
@@ -161,11 +161,14 @@ import { ScrollList } from './components';
addSystem(defineSystem(
[Res(Input), Query(Children, UIMask, ScrollList), Query(Mut(LocalTransform))],
(input, maskQuery, childQuery) => {
- for (const [entity, children, mask, scroll] of maskQuery) {
- const scroll = input.getScrollDelta();
- for (const child of children.entities) {
- const [, transform] = childQuery.get(child);
- transform.position.y += scroll.y * 20;
+ const scrollDelta = input.getScrollDelta();
+ if (scrollDelta.y === 0) return;
+
+ for (const [entity, children] of maskQuery) {
+ for (const [childEntity, transform] of childQuery) {
+ if (children.entities.includes(childEntity)) {
+ transform.position.y += scrollDelta.y * 20;
+ }
}
}
}
@@ -202,16 +205,18 @@ Adds a state machine on top of `Interactable` with optional color transitions.
When `transition` is set, the `Sprite` color is automatically updated on state change:
```typescript
-import { Button, Interactable } from 'esengine';
-
-world.insert(entity, Button, {
- state: 0,
- transition: {
- normalColor: { r: 1, g: 1, b: 1, a: 1 },
- hoveredColor: { r: 0.9, g: 0.9, b: 0.9, a: 1 },
- pressedColor: { r: 0.7, g: 0.7, b: 0.7, a: 1 },
- disabledColor: { r: 0.5, g: 0.5, b: 0.5, a: 0.5 },
- },
+import { defineSystem, Commands, Button, Interactable } from 'esengine';
+
+defineSystem([Commands()], (cmds) => {
+ cmds.entity(entity).insert(Button, {
+ state: 0,
+ transition: {
+ normalColor: { r: 1, g: 1, b: 1, a: 1 },
+ hoveredColor: { r: 0.9, g: 0.9, b: 0.9, a: 1 },
+ pressedColor: { r: 0.7, g: 0.7, b: 0.7, a: 1 },
+ disabledColor: { r: 0.5, g: 0.5, b: 0.5, a: 0.5 },
+ },
+ });
});
```
@@ -251,16 +256,19 @@ addSystem(defineSystem(
A tag component for root UI entities. Entities with `ScreenSpace`, `UIRect`, and `LocalTransform` are laid out relative to the camera bounds by `UILayoutPlugin`.
```typescript
-import { ScreenSpace, UIRect, LocalTransform } from 'esengine';
-
-world.insert(entity, ScreenSpace);
-world.insert(entity, UIRect, {
- anchorMin: { x: 0, y: 0 },
- anchorMax: { x: 1, y: 1 },
- offsetMin: { x: 0, y: 0 },
- offsetMax: { x: 0, y: 0 },
- size: { x: 100, y: 100 },
- pivot: { x: 0.5, y: 0.5 },
+import { defineSystem, Commands, ScreenSpace, UIRect, LocalTransform } from 'esengine';
+
+defineSystem([Commands()], (cmds) => {
+ cmds.entity(entity)
+ .insert(ScreenSpace)
+ .insert(UIRect, {
+ anchorMin: { x: 0, y: 0 },
+ anchorMax: { x: 1, y: 1 },
+ offsetMin: { x: 0, y: 0 },
+ offsetMax: { x: 0, y: 0 },
+ size: { x: 100, y: 100 },
+ pivot: { x: 0.5, y: 0.5 },
+ });
});
```
@@ -309,8 +317,11 @@ addSystem(defineSystem(
[Query(TextInput), Res(UIEvents)],
(query, events) => {
for (const e of events.query('submit')) {
- const [, input] = query.get(e.entity);
- console.log('Submitted:', input.value);
+ for (const [entity, input] of query) {
+ if (entity === e.entity) {
+ console.log('Submitted:', input.value);
+ }
+ }
}
}
));
diff --git a/docs/astro/src/content/docs/zh/core-concepts/systems.mdx b/docs/astro/src/content/docs/zh/core-concepts/systems.mdx
index 1fc5570ce..4e6063766 100644
--- a/docs/astro/src/content/docs/zh/core-concepts/systems.mdx
+++ b/docs/astro/src/content/docs/zh/core-concepts/systems.mdx
@@ -71,7 +71,9 @@ addSystemToSchedule(Schedule.FixedUpdate, physicsSystem);
在运行时创建、修改和销毁实体:
```typescript
-import { Commands, Sprite, LocalTransform } from 'esengine';
+import { Commands, Sprite, LocalTransform, Velocity, defineResource } from 'esengine';
+
+const Score = defineResource({ value: 0 });
defineSystem([Commands()], (cmds) => {
// 创建新实体并添加组件(可链式调用)
diff --git a/docs/astro/src/content/docs/zh/guides/plugins.mdx b/docs/astro/src/content/docs/zh/guides/plugins.mdx
index 98e645a42..3e2521143 100644
--- a/docs/astro/src/content/docs/zh/guides/plugins.mdx
+++ b/docs/astro/src/content/docs/zh/guides/plugins.mdx
@@ -65,7 +65,7 @@ const Enemy = defineTag('Enemy');
```typescript
import { defineSystem, addSystem, Schedule } from 'esengine';
-addSystem(defineSystem([], (world) => {
+addSystem(defineSystem([], () => {
// 每帧在 Update 阶段执行
}));
```
@@ -75,7 +75,7 @@ addSystem(defineSystem([], (world) => {
```typescript
import { addSystemToSchedule, defineSystem, Schedule } from 'esengine';
-addSystemToSchedule(Schedule.FixedUpdate, defineSystem([], (world) => {
+addSystemToSchedule(Schedule.FixedUpdate, defineSystem([], () => {
// 以固定时间步执行,适合物理逻辑
}));
```
@@ -85,7 +85,7 @@ addSystemToSchedule(Schedule.FixedUpdate, defineSystem([], (world) => {
```typescript
import { addStartupSystem, defineSystem } from 'esengine';
-addStartupSystem(defineSystem([], (world) => {
+addStartupSystem(defineSystem([], () => {
// 游戏启动时执行一次
}));
```
@@ -99,15 +99,15 @@ addStartupSystem(defineSystem([], (world) => {
资源是可在系统间共享的全局单例数据:
```typescript
-import { defineResource, addStartupSystem, defineSystem } from 'esengine';
+import { defineResource, addStartupSystem, defineSystem, Commands } from 'esengine';
const GameState = defineResource<{ score: number; level: number }>(
{ score: 0, level: 1 },
'GameState'
);
-addStartupSystem(defineSystem([], (world) => {
- world.app.insertResource(GameState, { score: 0, level: 1 });
+addStartupSystem(defineSystem([Commands()], (cmds) => {
+ cmds.insertResource(GameState, { score: 0, level: 1 });
}));
```
@@ -136,15 +136,15 @@ addStartupSystem(defineSystem([], (world) => {
插件是实现了 `Plugin` 接口的对象:
```typescript
-import { type Plugin, type App, defineResource, defineSystem, Schedule, registerComponent } from 'esengine';
+import { type Plugin, type App, defineResource, defineComponent, defineSystem, Schedule } from 'esengine';
const ScoreData = defineResource<{ value: number }>({ value: 0 }, 'Score');
+const Health = defineComponent('Health', { hp: 100, maxHp: 100 });
const myPlugin: Plugin = {
name: 'MyGamePlugin',
build(app: App) {
app.insertResource(ScoreData, { value: 0 });
- registerComponent('Health', { hp: 100, maxHp: 100 });
app.addSystemToSchedule(Schedule.Update, defineSystem([], () => {
// 游戏逻辑
}));
diff --git a/docs/astro/src/content/docs/zh/guides/spine.mdx b/docs/astro/src/content/docs/zh/guides/spine.mdx
index c1600be53..b5636591d 100644
--- a/docs/astro/src/content/docs/zh/guides/spine.mdx
+++ b/docs/astro/src/content/docs/zh/guides/spine.mdx
@@ -110,7 +110,7 @@ const CharacterState = defineComponent('CharacterState', {
});
addSystem(defineSystem(
- [Res(Input), Res(Time), Query(Mut(LocalTransform), Mut(SpineAnimation), CharacterState)],
+ [Res(Input), Res(Time), Query(Mut(LocalTransform), Mut(SpineAnimation), Mut(CharacterState))],
(input, time, query) => {
for (const [entity, transform, spine, state] of query) {
let moving = false;
diff --git a/docs/astro/src/content/docs/zh/guides/sprite.mdx b/docs/astro/src/content/docs/zh/guides/sprite.mdx
index 85d042ca2..6b3dc26da 100644
--- a/docs/astro/src/content/docs/zh/guides/sprite.mdx
+++ b/docs/astro/src/content/docs/zh/guides/sprite.mdx
@@ -102,7 +102,7 @@ const Animation = defineComponent('Animation', {
});
addSystem(defineSystem(
- [Res(Time), Query(Mut(Sprite), Animation)],
+ [Res(Time), Query(Mut(Sprite), Mut(Animation))],
(time, query) => {
for (const [entity, sprite, anim] of query) {
anim.elapsed += time.delta;
diff --git a/docs/astro/src/content/docs/zh/guides/ui.mdx b/docs/astro/src/content/docs/zh/guides/ui.mdx
index f8472edf0..dd86eef71 100644
--- a/docs/astro/src/content/docs/zh/guides/ui.mdx
+++ b/docs/astro/src/content/docs/zh/guides/ui.mdx
@@ -161,11 +161,14 @@ import { ScrollList } from './components';
addSystem(defineSystem(
[Res(Input), Query(Children, UIMask, ScrollList), Query(Mut(LocalTransform))],
(input, maskQuery, childQuery) => {
- for (const [entity, children, mask, scroll] of maskQuery) {
- const scroll = input.getScrollDelta();
- for (const child of children.entities) {
- const [, transform] = childQuery.get(child);
- transform.position.y += scroll.y * 20;
+ const scrollDelta = input.getScrollDelta();
+ if (scrollDelta.y === 0) return;
+
+ for (const [entity, children] of maskQuery) {
+ for (const [childEntity, transform] of childQuery) {
+ if (children.entities.includes(childEntity)) {
+ transform.position.y += scrollDelta.y * 20;
+ }
}
}
}
@@ -202,16 +205,18 @@ ESEngine 提供了内置的 UI 交互系统,用于处理 UI 元素的悬停、
设置 `transition` 后,状态变化时 `Sprite` 颜色会自动更新:
```typescript
-import { Button, Interactable } from 'esengine';
-
-world.insert(entity, Button, {
- state: 0,
- transition: {
- normalColor: { r: 1, g: 1, b: 1, a: 1 },
- hoveredColor: { r: 0.9, g: 0.9, b: 0.9, a: 1 },
- pressedColor: { r: 0.7, g: 0.7, b: 0.7, a: 1 },
- disabledColor: { r: 0.5, g: 0.5, b: 0.5, a: 0.5 },
- },
+import { defineSystem, Commands, Button, Interactable } from 'esengine';
+
+defineSystem([Commands()], (cmds) => {
+ cmds.entity(entity).insert(Button, {
+ state: 0,
+ transition: {
+ normalColor: { r: 1, g: 1, b: 1, a: 1 },
+ hoveredColor: { r: 0.9, g: 0.9, b: 0.9, a: 1 },
+ pressedColor: { r: 0.7, g: 0.7, b: 0.7, a: 1 },
+ disabledColor: { r: 0.5, g: 0.5, b: 0.5, a: 0.5 },
+ },
+ });
});
```
@@ -251,16 +256,19 @@ addSystem(defineSystem(
用于根 UI 实体的标签组件。拥有 `ScreenSpace`、`UIRect` 和 `LocalTransform` 的实体会由 `UILayoutPlugin` 相对于相机边界进行布局。
```typescript
-import { ScreenSpace, UIRect, LocalTransform } from 'esengine';
-
-world.insert(entity, ScreenSpace);
-world.insert(entity, UIRect, {
- anchorMin: { x: 0, y: 0 },
- anchorMax: { x: 1, y: 1 },
- offsetMin: { x: 0, y: 0 },
- offsetMax: { x: 0, y: 0 },
- size: { x: 100, y: 100 },
- pivot: { x: 0.5, y: 0.5 },
+import { defineSystem, Commands, ScreenSpace, UIRect, LocalTransform } from 'esengine';
+
+defineSystem([Commands()], (cmds) => {
+ cmds.entity(entity)
+ .insert(ScreenSpace)
+ .insert(UIRect, {
+ anchorMin: { x: 0, y: 0 },
+ anchorMax: { x: 1, y: 1 },
+ offsetMin: { x: 0, y: 0 },
+ offsetMax: { x: 0, y: 0 },
+ size: { x: 100, y: 100 },
+ pivot: { x: 0.5, y: 0.5 },
+ });
});
```
@@ -309,8 +317,11 @@ addSystem(defineSystem(
[Query(TextInput), Res(UIEvents)],
(query, events) => {
for (const e of events.query('submit')) {
- const [, input] = query.get(e.entity);
- console.log('提交内容:', input.value);
+ for (const [entity, input] of query) {
+ if (entity === e.entity) {
+ console.log('提交内容:', input.value);
+ }
+ }
}
}
));