From 38f4a1f47d3e4260c041db8f857cbb364a2997c3 Mon Sep 17 00:00:00 2001 From: panw Date: Fri, 8 May 2026 16:02:50 +0800 Subject: [PATCH] =?UTF-8?q?fix(ECS):=20=E5=BB=B6=E8=BF=9F=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=E6=9F=A5=E8=AF=A2=E5=8C=B9=E9=85=8D=E5=99=A8?= =?UTF-8?q?=E5=B9=B6=E6=B7=BB=E5=8A=A0=E7=A9=BA=E5=80=BC=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 避免在组件重置后访问未初始化的匹配器,将匹配器的初始化移至 onLoad 方法,并在清理和重置逻辑中添加空值保护,防止运行时错误。 --- assets/script/game/map/MissionComp.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/assets/script/game/map/MissionComp.ts b/assets/script/game/map/MissionComp.ts index dd632cb3..855cc23a 100644 --- a/assets/script/game/map/MissionComp.ts +++ b/assets/script/game/map/MissionComp.ts @@ -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(); }