fix(ECS): 延迟初始化查询匹配器并添加空值检查
避免在组件重置后访问未初始化的匹配器,将匹配器的初始化移至 onLoad 方法,并在清理和重置逻辑中添加空值保护,防止运行时错误。
This commit is contained in:
@@ -170,15 +170,18 @@ export class MissionComp extends CCComp {
|
|||||||
// ======================== ECS 查询匹配器(预缓存) ========================
|
// ======================== ECS 查询匹配器(预缓存) ========================
|
||||||
|
|
||||||
/** 匹配拥有 HeroViewComp 的实体(英雄/怪物视图) */
|
/** 匹配拥有 HeroViewComp 的实体(英雄/怪物视图) */
|
||||||
private readonly heroViewMatcher = ecs.allOf(HeroViewComp);
|
private heroViewMatcher: any = null;
|
||||||
/** 匹配拥有 SkillView 的实体(技能视图) */
|
/** 匹配拥有 SkillView 的实体(技能视图) */
|
||||||
private readonly skillViewMatcher = ecs.allOf(SkillView);
|
private skillViewMatcher: any = null;
|
||||||
/** 匹配拥有 HeroAttrsComp 的实体(英雄/怪物属性) */
|
/** 匹配拥有 HeroAttrsComp 的实体(英雄/怪物属性) */
|
||||||
private readonly heroAttrsMatcher = ecs.allOf(HeroAttrsComp);
|
private heroAttrsMatcher: any = null;
|
||||||
|
|
||||||
// ======================== 生命周期 ========================
|
// ======================== 生命周期 ========================
|
||||||
|
|
||||||
onLoad(){
|
onLoad(){
|
||||||
|
this.heroViewMatcher = ecs.allOf(HeroViewComp);
|
||||||
|
this.skillViewMatcher = ecs.allOf(SkillView);
|
||||||
|
this.heroAttrsMatcher = ecs.allOf(HeroAttrsComp);
|
||||||
this.showMemoryPanel = false
|
this.showMemoryPanel = false
|
||||||
// 注册生命周期事件
|
// 注册生命周期事件
|
||||||
this.on(GameEvent.MissionEnd,this.mission_end,this)
|
this.on(GameEvent.MissionEnd,this.mission_end,this)
|
||||||
@@ -904,6 +907,7 @@ export class MissionComp extends CCComp {
|
|||||||
|
|
||||||
/** 清理所有英雄和技能 ECS 实体 */
|
/** 清理所有英雄和技能 ECS 实体 */
|
||||||
private cleanComponents() {
|
private cleanComponents() {
|
||||||
|
if (!this.heroViewMatcher || !this.skillViewMatcher) return;
|
||||||
const heroEntities: ecs.Entity[] = [];
|
const heroEntities: ecs.Entity[] = [];
|
||||||
ecs.query(this.heroViewMatcher).forEach(entity => {
|
ecs.query(this.heroViewMatcher).forEach(entity => {
|
||||||
heroEntities.push(entity);
|
heroEntities.push(entity);
|
||||||
@@ -1059,6 +1063,9 @@ export class MissionComp extends CCComp {
|
|||||||
/** ECS 组件移除时销毁节点 */
|
/** ECS 组件移除时销毁节点 */
|
||||||
reset() {
|
reset() {
|
||||||
this.PhaseTime = null as any;
|
this.PhaseTime = null as any;
|
||||||
|
this.heroViewMatcher = null;
|
||||||
|
this.skillViewMatcher = null;
|
||||||
|
this.heroAttrsMatcher = null;
|
||||||
if (this.node && this.node.isValid) {
|
if (this.node && this.node.isValid) {
|
||||||
this.node.destroy();
|
this.node.destroy();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user