import { _decorator, Component, ProgressBar } from 'cc'; const { ccclass, property } = _decorator; @ccclass('LifeBar') export class LifeBar extends Component { _curHp: number = null; _totalHp: number = null; progressBar: ProgressBar = null; protected onLoad(): void { this.progressBar = this.node.getChildByName("ProgressBar").getComponent(ProgressBar); } start() { this.progressBar.progress = this._curHp / this._totalHp; } updateProgressBar(hp: number) { this._curHp = hp; this.progressBar.progress = this._curHp / this._totalHp; if (hp <= 0) { // this._role.playAnimation(RoleState.Die); this._curHp = 0; this.progressBar.progress = 0; } } }