fix(hero): 修复英雄视图组件的空指针异常和死亡逻辑

- 添加ent和model的安全检查防止空指针异常
- 重构死亡计时逻辑,使用deadCD代替Timer
- 统一死亡事件触发顺序并添加安全检查
- 在reset方法中清理碰撞器事件和伤害队列
This commit is contained in:
2025-11-03 16:32:30 +08:00
parent c98f20ba1d
commit 04aa5f9c78

View File

@@ -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,13 +312,10 @@ 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){
// 英雄死亡:延迟触发死亡事件
@@ -316,6 +323,15 @@ export class HeroViewComp extends CCComp {
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);