fix: 修复组件销毁时事件解绑和空引用问题并添加任务界面

- 在多个组件的onDestroy方法中添加节点有效性检查,防止无效节点上解绑事件
- 修复MissionComp中任务启动逻辑,改为通过UI打开方式触发MissionStart事件
- 添加新的任务界面(UIID.Mission)及相关配置
- 修复MissionCardComp中Map未初始化导致的空引用问题
- 优化按钮事件绑定和解绑逻辑,增加空值检查
This commit is contained in:
panw
2026-05-08 14:14:38 +08:00
parent c70e3bbb4d
commit 07aec09283
16 changed files with 19665 additions and 19580 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,13 @@
{
"ver": "1.1.50",
"importer": "prefab",
"imported": true,
"uuid": "60491d56-aa9b-4121-b9a7-4b146b58ec2e",
"files": [
".json"
],
"subMetas": {},
"userData": {
"syncNodeName": "mission"
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -20,6 +20,7 @@ export enum UIID {
Ranks,
Heros,
Talents,
Mission,
}
/** 打开界面方式的配置数据 */
@@ -33,4 +34,5 @@ export var UIConfigData: { [key: number]: UIConfig } = {
[UIID.Ranks]: { layer: LayerType.UI, prefab: "gui/element/ranks" },
[UIID.Heros]: { layer: LayerType.UI, prefab: "gui/element/heros" },
[UIID.Talents]: { layer: LayerType.UI, prefab: "gui/element/talents" },
[UIID.Mission]: { layer: LayerType.UI, prefab: "gui/element/mission" },
}

View File

@@ -513,12 +513,18 @@ export class CardComp extends CCComp {
/** 解绑触控,防止节点销毁后残留回调 */
private unbindEvents() {
this.node.off(NodeEventType.TOUCH_START, this.onCardTouchStart, this);
this.node.off(NodeEventType.TOUCH_MOVE, this.onCardTouchMove, this);
this.node.off(NodeEventType.TOUCH_END, this.onCardTouchEnd, this);
this.node.off(NodeEventType.TOUCH_CANCEL, this.onCardTouchCancel, this);
this.Lock?.off(NodeEventType.TOUCH_END, this.onToggleLock, this);
this.unLock?.off(NodeEventType.TOUCH_END, this.onToggleLock, this);
if (this.node && this.node.isValid) {
this.node.off(NodeEventType.TOUCH_START, this.onCardTouchStart, this);
this.node.off(NodeEventType.TOUCH_MOVE, this.onCardTouchMove, this);
this.node.off(NodeEventType.TOUCH_END, this.onCardTouchEnd, this);
this.node.off(NodeEventType.TOUCH_CANCEL, this.onCardTouchCancel, this);
}
if (this.Lock && this.Lock.isValid) {
this.Lock.off(NodeEventType.TOUCH_END, this.onToggleLock, this);
}
if (this.unLock && this.unLock.isValid) {
this.unLock.off(NodeEventType.TOUCH_END, this.onToggleLock, this);
}
}
// ======================== 触摸交互 ========================

View File

@@ -87,7 +87,10 @@ export class CardControllerComp extends CCComp {
*/
page_init(){
this.node.getChildByName("mission_home").active=true;
this.node.getChildByName("mission").active=false;
let missionNode = this.node.getChildByName("mission");
if (missionNode) {
missionNode.active = false;
}
}
/** ECS 组件移除时销毁节点 */

View File

@@ -79,7 +79,9 @@ export class IBoxComp extends CCComp {
/** 解绑点击事件 */
onDestroy() {
this.node.off(NodeEventType.TOUCH_END, this.onTapClose, this);
if (this.node && this.node.isValid) {
this.node.off(NodeEventType.TOUCH_END, this.onTapClose, this);
}
}
/** ECS 组件移除时销毁节点 */

View File

@@ -176,6 +176,11 @@ export class MissionCardComp extends CCComp {
/** 组件销毁时解绑所有事件并清理英雄信息面板 */
onDestroy() {
if (this.cards_chou && this.cards_chou.isValid) {
this.cards_chou.off(NodeEventType.TOUCH_START, this.onDrawTouchStart, this);
this.cards_chou.off(NodeEventType.TOUCH_END, this.onDrawTouchEnd, this);
this.cards_chou.off(NodeEventType.TOUCH_CANCEL, this.onDrawTouchCancel, this);
}
this.unbindEvents();
this.clearHeroInfoPanels();
}
@@ -205,6 +210,17 @@ export class MissionCardComp extends CCComp {
missionData.hero_max_num = FightSet.HERO_MAX_NUM;
missionData.hero_extend_max_num = FightSet.HERO_MAX_NUM + 1;
}
// 确保 Map 被正确初始化
if (!this.heroInfoItems) {
this.heroInfoItems = new Map();
}
// 确保卡牌组件列表已被正确缓存
if (!this.cardComps || this.cardComps.length === 0) {
this.cacheCardComps();
}
this.clearHeroInfoPanels();
this.layoutCardSlots();
this.clearAllCards();
@@ -362,9 +378,11 @@ export class MissionCardComp extends CCComp {
oops.message.off(GameEvent.UseHeroCard, this.onUseHeroCard, this);
oops.message.off(GameEvent.UseSpecialCard, this.onUseSpecialCard, this);
oops.message.off(GameEvent.CardPoolUpgrade, this.onCardPoolUpgrade, this);
this.cards_chou?.off(NodeEventType.TOUCH_START, this.onDrawTouchStart, this);
this.cards_chou?.off(NodeEventType.TOUCH_END, this.onDrawTouchEnd, this);
this.cards_chou?.off(NodeEventType.TOUCH_CANCEL, this.onDrawTouchCancel, this);
if (this.cards_chou && this.cards_chou.isValid) {
this.cards_chou.off(NodeEventType.TOUCH_START, this.onDrawTouchStart, this);
this.cards_chou.off(NodeEventType.TOUCH_END, this.onDrawTouchEnd, this);
this.cards_chou.off(NodeEventType.TOUCH_CANCEL, this.onDrawTouchCancel, this);
}
// this.cards_up?.off(NodeEventType.TOUCH_START, this.onUpgradeTouchStart, this);
// this.cards_up?.off(NodeEventType.TOUCH_END, this.onUpgradeTouchEnd, this);
// this.cards_up?.off(NodeEventType.TOUCH_CANCEL, this.onUpgradeTouchCancel, this);
@@ -700,26 +718,35 @@ export class MissionCardComp extends CCComp {
/** 全量分发给4槽每个槽位是否接收由 CardComp 自己判断(锁定可跳过) */
private dispatchCardsToSlots(cards: CardConfig[]) {
if (!this.cardComps) return;
for (let i = 0; i < this.cardComps.length; i++) {
const accepted = this.cardComps[i].applyDrawCard(cards[i] ?? null);
mLogger.log(this.debugMode, "MissionCardComp", "dispatch card", {
index: i,
card: cards[i]?.uuid ?? 0,
accepted
});
if (this.cardComps[i]) {
const accepted = this.cardComps[i].applyDrawCard(cards[i] ?? null);
mLogger.log(this.debugMode, "MissionCardComp", "dispatch card", {
index: i,
card: cards[i]?.uuid ?? 0,
accepted
});
}
}
}
/** 系统清空4槽用于任务切换 */
private clearAllCards() {
this.cardComps.forEach(comp => comp.clearBySystem());
if (!this.cardComps) return;
this.cardComps.forEach(comp => {
if (comp) comp.clearBySystem();
});
}
private layoutCardSlots() {
if (!this.cardComps) return;
const count = this.cardComps.length;
if (count === 0) return;
for (let i = 0; i < count; i++) {
this.cardComps[i].setSlotPosition(this.cardsPos[i]);
if (this.cardComps[i]) {
this.cardComps[i].setSlotPosition(this.cardsPos[i]);
}
}
mLogger.log(this.debugMode, "MissionCardComp", "layout card slots", {
count,
@@ -956,12 +983,14 @@ export class MissionCardComp extends CCComp {
}
private clearHeroInfoPanels() {
this.heroInfoItems.forEach(item => {
if (item.node && item.node.isValid) {
item.node.destroy();
}
});
this.heroInfoItems.clear();
if (this.heroInfoItems) {
this.heroInfoItems.forEach(item => {
if (item && item.node && item.node.isValid) {
item.node.destroy();
}
});
this.heroInfoItems.clear();
}
if (this.hero_info_node && this.hero_info_node.isValid) {
for (let i = this.hero_info_node.children.length - 1; i >= 0; i--) {
const child = this.hero_info_node.children[i];
@@ -1149,6 +1178,13 @@ export class MissionCardComp extends CCComp {
this.clearHeroInfoPanels();
this.resetButtonScale(this.cards_chou);
// this.resetButtonScale(this.cards_up);
this.node.destroy();
// 关键:在 reset/销毁 时将 Map 置空,彻底切断引用
this.heroInfoItems = null as any;
this.cardComps = [] as any;
if (this.node && this.node.isValid) {
this.node.destroy();
}
}
}

View File

@@ -181,7 +181,6 @@ export class MissionComp extends CCComp {
onLoad(){
this.showMemoryPanel = false
// 注册生命周期事件
this.on(GameEvent.MissionStart,this.mission_start,this)
this.on(GameEvent.MissionEnd,this.mission_end,this)
this.on(GameEvent.NewWave,this.onNewWave,this)
this.on(GameEvent.DO_AD_BACK,this.do_ad,this)
@@ -189,8 +188,16 @@ export class MissionComp extends CCComp {
this.removeMemoryPanel()
}
onAdded(args: any) {
// UI加载完成后抛出MissionStart事件启动战斗触发自身 mission_start 及其他系统)
oops.message.dispatchEvent(GameEvent.MissionStart, {});
this.mission_start();
}
onDestroy(){
this.start_btn?.off(NodeEventType.TOUCH_END, this.onStartFightBtnClick, this)
if (this.start_btn && this.start_btn.isValid) {
this.start_btn.off(NodeEventType.TOUCH_END, this.onStartFightBtnClick, this);
}
}
/**
@@ -295,15 +302,16 @@ export class MissionComp extends CCComp {
async mission_start(){
this.unscheduleAllCallbacks();
this.cleanComponents();
this.node.active=true
this.data_init()
oops.message.dispatchEvent(GameEvent.FightReady)
this.changePhase(MissionPhase.Prepare)
let loading=this.node.parent.getChildByName("loading")
loading.active=true
this.scheduleOnce(()=>{
loading.active=false
},0.5)
let loading=this.node.getChildByName("loading")
if (loading) {
loading.active=true
this.scheduleOnce(()=>{
loading.active=false
},0.5)
}
}
/** 更新开始按钮的状态显示 */
@@ -670,7 +678,7 @@ export class MissionComp extends CCComp {
this.changePhase(MissionPhase.None);
this.cleanComponents()
this.clearBattlePools()
this.node.active=false
oops.gui.remove(UIID.Mission);
}
/**

View File

@@ -98,9 +98,6 @@ export class MissionHeroCompComp extends CCComp {
// 清理全部监听
oops.message.off(GameEvent.CallHero,this.call_hero,this)
oops.message.off("PhasePrepareStart",this.fight_ready,this)
oops.message.off(GameEvent.FightReady,this.fight_ready,this)
oops.message.off(GameEvent.Zhaohuan,this.zhao_huan,this)
oops.message.off(GameEvent.MissionEnd,this.clear_heros,this)
}
start() {

View File

@@ -76,7 +76,7 @@ export class MissionHomeComp extends CCComp {
*/
start_mission() {
mLogger.log(this.debugMode, 'MissionHomeComp', "start_mission")
oops.message.dispatchEvent(GameEvent.MissionStart, {})
oops.gui.open(UIID.Mission)
this.node.active=false;
// 隐藏 mapLayer 下的 main 节点

View File

@@ -94,7 +94,9 @@ export class SkillBoxComp extends CCComp {
onDestroy() {
oops.message.off(GameEvent.FightStart, this.onFightStart, this);
oops.message.off(GameEvent.MissionEnd, this.onMissionEnd, this);
this.node.off(GameEvent.NewWave, this.onNewWave, this);
if (this.node && this.node.isValid) {
this.node.off(GameEvent.NewWave, this.onNewWave, this);
}
oops.message.off(GameEvent.NewWave, this.onNewWaveGlobal, this);
// 通知 MissSkillsComp 回收该节点占用的槽位
oops.message.dispatchEvent(GameEvent.RemoveSkillBox, this.node);

View File

@@ -78,10 +78,10 @@ export class TalentsComp extends CCComp {
protected onLoad(): void {
// 绑定按钮事件
if (this.btn_reset) {
if (this.btn_reset && this.btn_reset.node) {
this.btn_reset.node.on(Button.EventType.CLICK, this.onResetClicked, this);
}
if (this.btn_close) {
if (this.btn_close && this.btn_close.node) {
this.btn_close.node.on(Button.EventType.CLICK, this.onCloseClicked, this);
}
}
@@ -343,6 +343,12 @@ export class TalentsComp extends CCComp {
protected onDestroy(): void {
mLogger.log(this.debugMode, 'TalentsComp', "释放界面");
if (this.btn_reset && this.btn_reset.node && this.btn_reset.node.isValid) {
this.btn_reset.node.off(Button.EventType.CLICK, this.onResetClicked, this);
}
if (this.btn_close && this.btn_close.node && this.btn_close.node.isValid) {
this.btn_close.node.off(Button.EventType.CLICK, this.onCloseClicked, this);
}
}
/** ECS 组件移除时销毁节点 */

View File

@@ -34,6 +34,7 @@ import { CKind } from "../common/config/CardSet";
import { ScoreWeights } from "../common/config/ScoreSet";
import { HighlightSet, HighlightType, HighlightLevel } from "../common/config/HighlightSet";
import { mLogger } from "../common/Logger";
import { UIID } from "../common/config/GameUIConfig";
const { ccclass, property } = _decorator;
@@ -585,7 +586,7 @@ export class VictoryComp extends CCComp {
oops.message.dispatchEvent(GameEvent.MissionEnd)
this.node.getChildByName("loading").active=true
this.scheduleOnce(()=>{
oops.message.dispatchEvent(GameEvent.MissionStart)
oops.gui.open(UIID.Mission)
this.node.getChildByName("loading").active=false
oops.gui.removeByNode(this.node)
},0.5)

View File

@@ -182,13 +182,13 @@ export class SkillView extends CCComp {
/** 视图对象通过 ecs.Entity.remove(ModuleViewComp) 删除组件是触发组件处理自定义释放逻辑 */
reset() {
// 清理碰撞体事件监听
if (this.collider) {
if (this.collider && this.collider.isValid) {
this.collider.off(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
this.collider.enabled = false;
}
this.pendingDisableCollider = false;
this.isDisposing = false;
if (this.anim) {
if (this.anim && this.anim.isValid) {
this.anim.off(Animation.EventType.FINISHED, this.onAnimationFinished, this);
}
// 取消所有定时器

View File

@@ -16,8 +16,9 @@ export class oneCom extends Component {
}
onDestroy() {
if (!this.anim) return;
this.anim.off(Animation.EventType.FINISHED, this.onAnimationFinished, this);
if (this.anim && this.anim.isValid) {
this.anim.off(Animation.EventType.FINISHED, this.onAnimationFinished, this);
}
this.anim = null;
}