From 04aa5f9c788cc730627d5b609d67e802143ddd80 Mon Sep 17 00:00:00 2001 From: panw Date: Mon, 3 Nov 2025 16:32:30 +0800 Subject: [PATCH] =?UTF-8?q?fix(hero):=20=E4=BF=AE=E5=A4=8D=E8=8B=B1?= =?UTF-8?q?=E9=9B=84=E8=A7=86=E5=9B=BE=E7=BB=84=E4=BB=B6=E7=9A=84=E7=A9=BA?= =?UTF-8?q?=E6=8C=87=E9=92=88=E5=BC=82=E5=B8=B8=E5=92=8C=E6=AD=BB=E4=BA=A1?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加ent和model的安全检查防止空指针异常 - 重构死亡计时逻辑,使用deadCD代替Timer - 统一死亡事件触发顺序并添加安全检查 - 在reset方法中清理碰撞器事件和伤害队列 --- assets/script/game/hero/HeroViewComp.ts | 55 ++++++++++++++++++------- 1 file changed, 39 insertions(+), 16 deletions(-) diff --git a/assets/script/game/hero/HeroViewComp.ts b/assets/script/game/hero/HeroViewComp.ts index deb3b598..8a066392 100644 --- a/assets/script/game/hero/HeroViewComp.ts +++ b/assets/script/game/hero/HeroViewComp.ts @@ -32,12 +32,18 @@ export class HeroViewComp extends CCComp { box_group:number = BoxSet.HERO; // 碰撞组 usePower:boolean = false; useMp:boolean = false; - deadTime:Timer=new Timer(10) + realDeadTime:number=10 + deadCD:number=0 // ==================== UI 节点引用 ==================== private top_node: Node = null!; // ==================== 直接访问 HeroAttrsComp ==================== get model() { + // 🔥 修复:添加安全检查,防止ent为null时的访问异常 + if (!this.ent) { + console.warn("[HeroViewComp] ent is null, returning null for model"); + return null; + } return this.ent.get(HeroAttrsComp); } @@ -102,10 +108,15 @@ export class HeroViewComp extends CCComp { update(dt: number){ if(!smc.mission.play || smc.mission.pause) return; - // 添加安全检查,防止在实体销毁过程中访问null的model + // 🔥 修复:添加安全检查,防止在实体销毁过程中访问null的model + if(!this.ent) return; if (!this.model) return; if(this.model.is_dead){ - if( this.deadTime.update(dt)) this.realDead() + this.deadCD+=dt + if(this.deadCD>=this.realDeadTime){ + this.deadCD=0 + this.realDead() + } return } ; // ✅ View 层职责:处理表现相关的逻辑 @@ -277,7 +288,6 @@ export class HeroViewComp extends CCComp { alive(){ this.model.is_dead=false - this.deadTime.reset() this.as.do_buff(); this.status_change("idle"); this.model.hp =this.model.Attrs[Attrs.HP_MAX]*50/100; @@ -302,20 +312,26 @@ export class HeroViewComp extends CCComp { } realDead(){ - // 根据阵营触发不同事件 - if(this.model.fac === FacSet.MON){ - oops.message.dispatchEvent(GameEvent.MonDead, { - hero_uuid: this.model.hero_uuid, - position: this.node.position - }); - this.ent.destroy(); - } + // 🔥 修复:添加model安全检查,防止实体销毁过程中的空指针异常 + if (!this.model) { + console.warn("[HeroViewComp] realDead called but model is null, skipping"); + return; + } if(this.model.fac === FacSet.HERO){ // 英雄死亡:延迟触发死亡事件 oops.message.dispatchEvent(GameEvent.HeroDead, { hero_uuid: this.model.hero_uuid }); } + // 根据阵营触发不同事件 + if(this.model.fac === FacSet.MON){ + oops.message.dispatchEvent(GameEvent.MonDead, { + hero_uuid: this.model.hero_uuid, + position: this.node.position + }); + } + this.ent.destroy(); + } do_atked(damage:number,isCrit:boolean,s_uuid:number){ if (damage <= 0) return; @@ -426,11 +442,18 @@ export class HeroViewComp extends CCComp { } } reset() { + // 清理碰撞器事件监听 const collider = this.getComponent(Collider2D); - if (collider) { - collider.off(Contact2DType.BEGIN_CONTACT); - } - this.deadTime.reset() + if (collider) { + collider.off(Contact2DType.BEGIN_CONTACT); + } + this.deadCD=0 + + // 清理伤害队列 + this.damageQueue.length = 0; + this.isProcessingDamage = false; + + // 延迟销毁节点 this.scheduleOnce(() => { this.node.destroy(); }, 0.1);