XForge2-Cocos/.trae/skills/xforge2-manual/SKILL.md
Detailed documentation and usage guide for the XForge2 framework. Invoke when implementing features, checking API usage, or needing examples for modules/services/views.
npx skillsauth add a1076559139/xforge2 xforge2-manualInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
extensions/xforge/runtime/ (框架源码)extensions/pkg/ (扩展包插件)assets/ (游戏逻辑)assets/)项目采用模块化设计,主要分为三类目录:
| 目录 | 说明 | 关键文件/用途 |
| :--- | :--- | :--- |
| assets/app | 全局入口 | app.ts (静态数据), main/Main.ts (启动脚本) |
| assets/app-global | 全局模块 | 通用功能(如通用弹窗等)。代码在 assembly/,资源在 assetbundle/。 |
| assets/app-module | 功能模块 | 独立功能(如 Home, Battle)。代码在 assembly/,资源在 assetbundle/。 |
| assets/app-shared | 共享资源 | 存放全局通用的代码和资源。 |
全局模块 (app-global) 与功能模块 (app-module) 逻辑结构一致,但命名习惯有别:
| 层级/功能 | 全局模块 (app-global) | 功能模块 (app-module) |
| :--- | :--- | :--- |
| 物理路径 | assets/app-global/ | assets/app-module/[ModuleName]/ |
| 入口文件 | assembly/AppGlobal.ts | assembly/AppModule[Name].ts |
| 装饰器 | @global | @module('[Name]') |
| 继承父类 | BaseModule | BaseModule |
| UI 组件 | assembly/global-view/ | assembly/module-view/ |
| 数据模型 | assembly/global-model/ | assembly/module-model/ |
| 业务逻辑 | assembly/global-service/ | assembly/module-service/ |
| 资源-View | assetbundle/global-view/ | assetbundle/module-view/ |
| 资源-Sound| assetbundle/global-sound/ | assetbundle/module-sound/ |
💡 注意:
app-module下需先创建模块名文件夹(如Home),再建立assembly和assetbundle。
模块下的 assembly (代码) 和 assetbundle (资源) 目录在 Cocos Creator 中都被配置为 AssetBundle。
app-global) 引用后加载的 AssetBundle (如 app-module/Home) 中的脚本”。
AppGlobal 引用 HomeService。HomeService 引用 AppGlobal (下层引用上层)。框架中的模块、服务、模型、视图、音乐音效等的创建都需要通过菜单工具来完成。
app)框架导出了全局变量 app (extensions/xforge/runtime/XForge.ts):
app.lib: 内置工具箱。app.global: 访问全局模块实例 (单例)。app.loadModule / app.unloadModule: 加载/卸载功能模块。app 对象不提供获取功能模块实例的 API。功能模块实例仅能在其内部组件中通过 this.module 访问,以保证解耦。启动流程主要由 assets/app/main/Main.ts 控制。
Main.ts 的核心职责:
setup() 方法进行框架初始化。启动步骤示例:
protected start(): void {
// 1. (必须) 框架初始化
this.setup();
// 2. 业务初始化流程 (推荐使用 task 队列,也可以直接 async/await)
app.lib.task.createSync()
.add(next => {
// 加载全局模块
app.loadGlobal({ onLoaded: next });
})
.add(next => {
// 进入首屏模块
app.loadModule({ name: 'Home', onLoaded: next });
})
.start(() => {
// 3. 销毁闪屏
this.splashScreen.destroy();
});
}
💡 提示: 启动逻辑不强制要求写在
start中,onLoad或onEnable亦可,但务必保证在进行任何业务操作前先调用this.setup()。
模块是核心组织单位,所有业务逻辑都必须归属于某个模块。
所有模块入口脚本都继承自 BaseModule,并使用装饰器标记。
// AppModuleHome.ts
import { BaseModule, module } from 'db://xforge/base/BaseModule';
@module('Home') // 功能模块
export class AppModuleHome extends BaseModule { ... }
// AppGlobal.ts
import { BaseModule, global } from 'db://xforge/base/BaseModule';
@global() // 全局模块
export class AppGlobal extends BaseModule { ... }
init(onLoaded, onError, onProgress): 模块初始化(异步)。onLoad(): 模块加载完成。onUnload(): 模块卸载。为了避免模块切换过程中出现黑屏,必须在 init 中调用 ui.show 并等待 onShow 回调后再执行 onLoaded()。
protected init(onLoaded: () => void, onError: () => void, onProgress: (result: number) => void): void {
// ✅ 正确:在 init 中显示 UI,并等待 onShow 后再 onLoaded()
this.ui.show({
view: PageHome,
onShow: onLoaded,
onError: onError,
onProgress: onProgress
});
}
UIManager)模块提供了 ui 对象用于管理界面显示。
// 打开界面
this.ui.show({
view: PageHome,
data: { id: 1 },
onShow: () => console.log('Opened'),
onHide: () => console.log('Closed')
});
SoundManager)模块提供了 sound 对象用于管理音频。
// 播放 BGM (循环) -> assets/.../module-sound/music/bgm/main.mp3
this.sound.playMusic('bgm/main');
// 播放音效 (单次) -> assets/.../module-sound/effect/ui/click.mp3
this.sound.playEffect('ui/click');
music/ 或 effect/ 目录。在模块内部的 Model/Service/View 中,可以通过 this.module 获取当前模块的 Model 和 Service 实例。
// 获取 Model
const gameModel = this.module.useModel(GameModel);
// 获取 Service
const gameService = this.module.useService(GameService);
Model 负责数据的存储、运算和校验,推荐结合 cc-store 实现响应式更新。
继承自 BaseModel。如果需要响应式能力,需在构造函数返回 createStore(this)。
// GameData.ts
import { BaseModel } from 'db://xforge/base/BaseModel';
import { IModelContext } from 'db://xforge/base/BaseModule';
import { createStore } from 'db://pkg/@gamex/cc-store';
export class GameData extends BaseModel {
constructor(module: IModelContext) {
super(module);
return createStore(this); // 启用响应式
}
score = 0;
}
cc.Node 等渲染类。在同模块的 Service/View 中:
const gameData = this.module.useModel(GameData);
Service 是模块的“大脑”,负责业务逻辑编排和跨组件通信。
继承自 BaseService。
import { BaseService } from 'db://xforge/base/BaseService';
export class GameService extends BaseService { ... }
cc.Node)。MessageBus)每个 Service 实例自带 event 属性。
⚠️ 严格约束:
// GameService.ts
export class LoginEvent implements MessageBus.IEvent { ... } // 1. 定义
export class GameService extends BaseService {
login() {
this.event.emit(new LoginEvent()); // 2. 发送
}
}
View 负责界面展示和交互,推荐使用 MVVM 模式。
继承自 BaseView。
static beforeShow(module, data): (静态方法/异步) 视图显示前的预加载阶段。onLoad(): 节点首次初始化时触发(引擎原生)。onShow(data): 视图每次显示时触发,接收外部传入的数据。beforeHide(): 视图隐藏前触发。onHide(): 视图隐藏后触发。限制:beforeShow中不要调用module.ui.show,避免show流程卡死
使用 cc-store 将 View 与 Model 绑定。
// PageGame.ts
import { bindStore, watchStore, stopWatch } from 'db://pkg/@gamex/cc-store';
export class PageGame extends BaseView {
onShow() {
const gameData = this.module.useModel(GameData);
// 属性绑定
bindStore(this.label, 'string', () => `Score: ${gameData.score}`);
// 逻辑监听
watchStore(this.onScoreChange, this);
}
onHide() {
// 停止监听(必须要手动停止watchStore,避免内存泄漏)
stopWatch(this.onScoreChange, this);
}
}
在 View 的脚本组件中,可以配置以下重要属性:
ViewHideMode.Active: 仅设置 node.active = false。保留节点内存,再次打开速度快。ViewHideMode.Destroy: 销毁节点。节省内存,但重新打开需要重新实例化。位于 app.lib 命名空间下。
app.lib.storage.set('key', 'val') / setDay (按天存储)。app.lib.task.createSync() 串行任务队列。app.lib.loader.loadBundleAsync / loadDir。app.lib.logger.log/warn/error。app.lib.debug.unobservable。所有扩展包安装在 extensions/pkg/。
node extensions/pkg/index.js add <package-name>import ... from 'db://pkg/<package-name>'框架基于 npm 管理,但建议仅使用以下经过框架适配的官方扩展包:
| 包名 | 说明 |
| :--- | :--- |
| @gamex/cc-expand | 属性扩展: node.x、node.scaleX等 |
| @gamex/cc-store | 状态管理,数据变化自动更新UI |
| @gamex/cc-request | POST/GET网络请求 |
| @gamex/cc-number | 防内存挂数字类型 |
| @gamex/cc-random | 种子随机 |
| @gamex/cc-astar | A星巡路,支持4/6/8方向及路径平滑 |
| @gamex/cc-quadtree | 四叉碰撞树 |
| @gamex/cc-sap | SAP碰撞检测 |
| @gamex/cc-sat | SAT碰撞检测 |
| @gamex/cc-rvo2 | 动态避障 |
| @gamex/cc-ecs | 实体-组件-系统 |
| @gamex/cc-emath | 精确数学运算,替换原生Math下三角函数运算并添加随机种子能力 |
| @gamex/cc-decimal | 定点数学运算 |
| @gamex/cc-decimal-vec2 | 定点数二维向量 |
| @gamex/cc-decimal-vec3 | 定点数三维向量 |
| @gamex/cc-decimal-sat | 定点数SAT碰撞检测 |
| @gamex/cc-decimal-sap | 定点数SAP碰撞检测 |
| @gamex/cc-decimal-random | 定点数随机 |
| @gamex/cc-xml-parser | XML解析 |
| @gamex/cc-minisdk | 小游戏SDK模块 |
| 包名 | 说明 |
| :--- | :--- |
| @gamex/cc-comp-toggle | Toggle组件 |
| @gamex/cc-comp-rich-text | RichText组件 |
| @gamex/cc-comp-spring-arm | 弹簧臂组件 |
| @gamex/cc-comp-animation | Animation组件 |
| @gamex/cc-comp-skeleton | Spine组件 |
| @gamex/cc-comp-skeletal-animation | 3D骨骼动画组件 |
| @gamex/cc-comp-movie-animation | MovieClip播放组件 |
| @gamex/cc-comp-frame-animation | 帧动画播放组件 |
| @gamex/cc-comp-rewardfly | 奖励飞行动画组件 |
| 包名 | 说明 |
| :--- | :--- |
| @gamex/cc-ctrl-toast | 消息提示控件 |
| @gamex/cc-ctrl-rocker | 摇杆控件 |
development
Maintainer-only workflow for handling GitHub Secret Scanning alerts on OpenClaw. Use when Codex needs to triage, redact, clean up, and resolve secret leakage found in issue comments, issue bodies, PR comments, or other GitHub content.
development
Maintainer workflow for OpenClaw releases, prereleases, changelog release notes, and publish validation. Use when Codex needs to prepare or verify stable or beta release steps, align version naming, assemble release notes, check release auth requirements, or validate publish-time commands and artifacts.
development
Run, watch, debug, and extend OpenClaw QA testing with qa-lab and qa-channel. Use when Codex needs to execute the repo-backed QA suite, inspect live QA artifacts, debug failing scenarios, add new QA scenarios, or explain the OpenClaw QA workflow. Prefer the live OpenAI lane with regular openai/gpt-5.4 in fast mode; do not use gpt-5.4-pro or gpt-5.4-mini unless the user explicitly overrides that policy.
development
End-to-end Parallels smoke, upgrade, and rerun workflow for OpenClaw across macOS, Windows, and Linux guests. Use when Codex needs to run, rerun, debug, or interpret VM-based install, onboarding, gateway smoke tests, latest-release-to-main upgrade checks, fresh snapshot retests, or optional Discord roundtrip verification under Parallels.