fix(mission): 修复组件生命周期与节点安全访问问题
- 移除 MissionCardComp.onLoad 中自动触发的 onMissionStart,改为由 MissionComp 通过 scheduleOnce 事件驱动,确保组件初始化顺序 - 在设置节点 active 属性前增加节点有效性检查,避免节点已销毁时访问导致的错误 - 在更新计时器 UI 时增加节点和组件存在性检查,防止空引用异常
This commit is contained in:
@@ -167,7 +167,7 @@ export class MissionCardComp extends CCComp {
|
|||||||
this.cacheCardComps();
|
this.cacheCardComps();
|
||||||
this.layoutCardSlots();
|
this.layoutCardSlots();
|
||||||
this.initCardsPanelPos();
|
this.initCardsPanelPos();
|
||||||
this.onMissionStart();
|
// this.onMissionStart(); // 移除 onLoad 自动触发,改为事件驱动
|
||||||
mLogger.log(this.debugMode, "MissionCardComp", "onLoad init", {
|
mLogger.log(this.debugMode, "MissionCardComp", "onLoad init", {
|
||||||
slots: this.cardComps.length,
|
slots: this.cardComps.length,
|
||||||
poolLv: this.poolLv
|
poolLv: this.poolLv
|
||||||
@@ -231,7 +231,9 @@ export class MissionCardComp extends CCComp {
|
|||||||
// this.resetButtonScale(this.cards_up);
|
// this.resetButtonScale(this.cards_up);
|
||||||
this.updateCoinAndCostUI();
|
this.updateCoinAndCostUI();
|
||||||
this.updateHeroNumUI(false, false);
|
this.updateHeroNumUI(false, false);
|
||||||
this.node.active = true;
|
if (this.node && this.node.isValid) {
|
||||||
|
this.node.active = true;
|
||||||
|
}
|
||||||
const cards = this.buildDrawCards();
|
const cards = this.buildDrawCards();
|
||||||
this.dispatchCardsToSlots(cards);
|
this.dispatchCardsToSlots(cards);
|
||||||
mLogger.log(this.debugMode, "MissionCardComp", "mission start", {
|
mLogger.log(this.debugMode, "MissionCardComp", "mission start", {
|
||||||
@@ -243,7 +245,9 @@ export class MissionCardComp extends CCComp {
|
|||||||
onMissionEnd() {
|
onMissionEnd() {
|
||||||
this.clearAllCards();
|
this.clearAllCards();
|
||||||
this.clearHeroInfoPanels();
|
this.clearHeroInfoPanels();
|
||||||
this.node.active = false;
|
if (this.node && this.node.isValid) {
|
||||||
|
this.node.active = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
start() {
|
start() {
|
||||||
@@ -264,7 +268,9 @@ export class MissionCardComp extends CCComp {
|
|||||||
|
|
||||||
/** 关闭面板(不销毁数据模型,仅隐藏) */
|
/** 关闭面板(不销毁数据模型,仅隐藏) */
|
||||||
close() {
|
close() {
|
||||||
this.node.active = false;
|
if (this.node && this.node.isValid) {
|
||||||
|
this.node.active = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ======================== 事件绑定 ========================
|
// ======================== 事件绑定 ========================
|
||||||
|
|||||||
@@ -189,9 +189,12 @@ export class MissionComp extends CCComp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onAdded(args: any) {
|
onAdded(args: any) {
|
||||||
// UI加载完成后,抛出MissionStart事件,启动战斗(触发自身 mission_start 及其他系统)
|
// 使用 scheduleOnce 将事件推迟到下一帧执行,
|
||||||
oops.message.dispatchEvent(GameEvent.MissionStart, {});
|
// 确保所有关联组件(如 MissionCardComp 等)都已经完成其 onLoad 生命周期。
|
||||||
this.mission_start();
|
this.scheduleOnce(() => {
|
||||||
|
oops.message.dispatchEvent(GameEvent.MissionStart, {});
|
||||||
|
this.mission_start();
|
||||||
|
}, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
onDestroy(){
|
onDestroy(){
|
||||||
@@ -244,7 +247,13 @@ export class MissionComp extends CCComp {
|
|||||||
if (this.isBossWave) {
|
if (this.isBossWave) {
|
||||||
const str = "∞";
|
const str = "∞";
|
||||||
if (str != this.lastTimeStr) {
|
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.lastTimeStr = str;
|
||||||
this.lastTimeSecond = -1;
|
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);
|
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')}`;
|
let str = `${m.toString().padStart(2, '0')}:${s.toString().padStart(2, '0')}`;
|
||||||
if(str != this.lastTimeStr){
|
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.lastTimeStr = str;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user