fix(mission): 修复组件生命周期与节点安全访问问题

- 移除 MissionCardComp.onLoad 中自动触发的 onMissionStart,改为由 MissionComp 通过 scheduleOnce 事件驱动,确保组件初始化顺序
- 在设置节点 active 属性前增加节点有效性检查,避免节点已销毁时访问导致的错误
- 在更新计时器 UI 时增加节点和组件存在性检查,防止空引用异常
This commit is contained in:
panw
2026-05-08 15:09:06 +08:00
parent 07aec09283
commit 64168e576f
2 changed files with 30 additions and 9 deletions

View File

@@ -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;
}
}