fix(ECS): 延迟初始化查询匹配器并添加空值检查

避免在组件重置后访问未初始化的匹配器,将匹配器的初始化移至 onLoad 方法,并在清理和重置逻辑中添加空值保护,防止运行时错误。
This commit is contained in:
panw
2026-05-08 16:02:50 +08:00
parent cc60ebf332
commit 38f4a1f47d

View File

@@ -170,15 +170,18 @@ export class MissionComp extends CCComp {
// ======================== ECS 查询匹配器(预缓存) ========================
/** 匹配拥有 HeroViewComp 的实体(英雄/怪物视图) */
private readonly heroViewMatcher = ecs.allOf(HeroViewComp);
private heroViewMatcher: any = null;
/** 匹配拥有 SkillView 的实体(技能视图) */
private readonly skillViewMatcher = ecs.allOf(SkillView);
private skillViewMatcher: any = null;
/** 匹配拥有 HeroAttrsComp 的实体(英雄/怪物属性) */
private readonly heroAttrsMatcher = ecs.allOf(HeroAttrsComp);
private heroAttrsMatcher: any = null;
// ======================== 生命周期 ========================
onLoad(){
this.heroViewMatcher = ecs.allOf(HeroViewComp);
this.skillViewMatcher = ecs.allOf(SkillView);
this.heroAttrsMatcher = ecs.allOf(HeroAttrsComp);
this.showMemoryPanel = false
// 注册生命周期事件
this.on(GameEvent.MissionEnd,this.mission_end,this)
@@ -904,6 +907,7 @@ export class MissionComp extends CCComp {
/** 清理所有英雄和技能 ECS 实体 */
private cleanComponents() {
if (!this.heroViewMatcher || !this.skillViewMatcher) return;
const heroEntities: ecs.Entity[] = [];
ecs.query(this.heroViewMatcher).forEach(entity => {
heroEntities.push(entity);
@@ -1059,6 +1063,9 @@ export class MissionComp extends CCComp {
/** ECS 组件移除时销毁节点 */
reset() {
this.PhaseTime = null as any;
this.heroViewMatcher = null;
this.skillViewMatcher = null;
this.heroAttrsMatcher = null;
if (this.node && this.node.isValid) {
this.node.destroy();
}