僵尸短时间被多个植物击中后,血量显示出现异常,包括:
在 HPBarAnimation.ts 中,当敌人短时间内多次受伤时:
playDamageAnimation() 都会创建新的 tween 动画在 EnemyInstance.ts 中:
private currentTween: any = null; // 当前正在运行的动画
private playDamageAnimation(newProgress: number) {
// 停止当前正在运行的动画,避免动画冲突
if (this.currentTween) {
this.currentTween.stop();
this.currentTween = null;
}
// ... 其他代码
// 保存动画引用
this.currentTween = tween({ progress: originalProgress })
// ... 动画配置
.start();
}
onDestroy() {
// 清理正在运行的动画,防止内存泄漏
if (this.currentTween) {
this.currentTween.stop();
this.currentTween = null;
}
}
takeDamage(damage: number, isCritical: boolean = false) {
// 确保伤害值为正数
if (damage <= 0) {
console.warn(`[EnemyInstance] 无效的伤害值: ${damage}`);
return;
}
// 计算新的血量,确保不会低于0
const newHealth = Math.max(0, this.health - damage);
const actualDamage = this.health - newHealth;
this.health = newHealth;
// ... 其他代码
}
updateHealthDisplay() {
// 确保血量值在有效范围内
this.health = Math.max(0, Math.min(this.maxHealth, this.health));
const healthProgress = this.maxHealth > 0 ? this.health / this.maxHealth : 0;
// 显示整数血量值
label.string = Math.ceil(this.health).toString();
}
使用提供的测试脚本 test_enemy_health_fix.js:
在控制台运行测试函数:
testMultipleDamage();
观察敌人血量显示是否正常
检查血条动画是否流畅
assets/scripts/Animations/HPBarAnimation.ts - 血条动画组件assets/scripts/CombatSystem/EnemyInstance.ts - 敌人实例组件test_enemy_health_fix.js - 测试脚本