From 64168e576f2fc1eb068fc10fdf5e59db1300389a Mon Sep 17 00:00:00 2001 From: panw Date: Fri, 8 May 2026 15:09:06 +0800 Subject: [PATCH] =?UTF-8?q?fix(mission):=20=E4=BF=AE=E5=A4=8D=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=E7=94=9F=E5=91=BD=E5=91=A8=E6=9C=9F=E4=B8=8E=E8=8A=82?= =?UTF-8?q?=E7=82=B9=E5=AE=89=E5=85=A8=E8=AE=BF=E9=97=AE=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除 MissionCardComp.onLoad 中自动触发的 onMissionStart,改为由 MissionComp 通过 scheduleOnce 事件驱动,确保组件初始化顺序 - 在设置节点 active 属性前增加节点有效性检查,避免节点已销毁时访问导致的错误 - 在更新计时器 UI 时增加节点和组件存在性检查,防止空引用异常 --- assets/script/game/map/MissionCardComp.ts | 14 +++++++++---- assets/script/game/map/MissionComp.ts | 25 ++++++++++++++++++----- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/assets/script/game/map/MissionCardComp.ts b/assets/script/game/map/MissionCardComp.ts index 82d1e467..cd4bfc3a 100644 --- a/assets/script/game/map/MissionCardComp.ts +++ b/assets/script/game/map/MissionCardComp.ts @@ -167,7 +167,7 @@ export class MissionCardComp extends CCComp { this.cacheCardComps(); this.layoutCardSlots(); this.initCardsPanelPos(); - this.onMissionStart(); + // this.onMissionStart(); // 移除 onLoad 自动触发,改为事件驱动 mLogger.log(this.debugMode, "MissionCardComp", "onLoad init", { slots: this.cardComps.length, poolLv: this.poolLv @@ -231,7 +231,9 @@ export class MissionCardComp extends CCComp { // this.resetButtonScale(this.cards_up); this.updateCoinAndCostUI(); this.updateHeroNumUI(false, false); - this.node.active = true; + if (this.node && this.node.isValid) { + this.node.active = true; + } const cards = this.buildDrawCards(); this.dispatchCardsToSlots(cards); mLogger.log(this.debugMode, "MissionCardComp", "mission start", { @@ -243,7 +245,9 @@ export class MissionCardComp extends CCComp { onMissionEnd() { this.clearAllCards(); this.clearHeroInfoPanels(); - this.node.active = false; + if (this.node && this.node.isValid) { + this.node.active = false; + } } start() { @@ -264,7 +268,9 @@ export class MissionCardComp extends CCComp { /** 关闭面板(不销毁数据模型,仅隐藏) */ close() { - this.node.active = false; + if (this.node && this.node.isValid) { + this.node.active = false; + } } // ======================== 事件绑定 ======================== diff --git a/assets/script/game/map/MissionComp.ts b/assets/script/game/map/MissionComp.ts index af7ec7d6..6db07d01 100644 --- a/assets/script/game/map/MissionComp.ts +++ b/assets/script/game/map/MissionComp.ts @@ -189,9 +189,12 @@ export class MissionComp extends CCComp { } onAdded(args: any) { - // UI加载完成后,抛出MissionStart事件,启动战斗(触发自身 mission_start 及其他系统) - oops.message.dispatchEvent(GameEvent.MissionStart, {}); - this.mission_start(); + // 使用 scheduleOnce 将事件推迟到下一帧执行, + // 确保所有关联组件(如 MissionCardComp 等)都已经完成其 onLoad 生命周期。 + this.scheduleOnce(() => { + oops.message.dispatchEvent(GameEvent.MissionStart, {}); + this.mission_start(); + }, 0); } onDestroy(){ @@ -244,7 +247,13 @@ export class MissionComp extends CCComp { if (this.isBossWave) { const str = "∞"; if (str != this.lastTimeStr) { - this.time_node.getChildByName("time").getComponent(Label).string = str; + if (this.time_node && this.time_node.isValid) { + const timeChild = this.time_node.getChildByName("time"); + if (timeChild) { + const label = timeChild.getComponent(Label); + if (label) label.string = str; + } + } this.lastTimeStr = str; this.lastTimeSecond = -1; } @@ -260,7 +269,13 @@ export class MissionComp extends CCComp { const wave = Math.max(1, this.currentWave || smc.vmdata.mission_data.level || 1); let str = `${m.toString().padStart(2, '0')}:${s.toString().padStart(2, '0')}`; if(str != this.lastTimeStr){ - this.time_node.getChildByName("time").getComponent(Label).string = str; + if (this.time_node && this.time_node.isValid) { + const timeChild = this.time_node.getChildByName("time"); + if (timeChild) { + const label = timeChild.getComponent(Label); + if (label) label.string = str; + } + } this.lastTimeStr = str; } }