const { ccclass, property } = cc._decorator; /** * 数字跳动动画 * @author 薛鸿潇 kaka */ @ccclass export default class NumberAnim extends cc.Component { /** 动画目标 */ private lbl_count: cc.Label = null; onLoad() { this.lbl_count = this.node.getComponent(cc.Label) || this.node.addComponent(cc.Label); } /** * 设置数值 * @param new_value 目标值 */ public setValue(new_value: number, toFixed: number = 0) { if (this.targetProgress == new_value) return; this.toFixed = toFixed; this.targetProgress = new_value; this.PlayMoneyAni(0.3); } //////////////////////////////////数字滚动效果////////////////////////////////// /** 滚动的目标值 */ private targetProgress: number = 0; private delta: number = 0; /** 滚动中的数字 */ private curValue: number = 0; /** 上次值 */ private lastNum: number = 0; /** 动画中 */ private isPlayAni: boolean = false; /** 保留小数点位数 */ private toFixed: number = 0; private PlayMoneyAni(time: number) { this.delta = (this.targetProgress - this.lastNum);// / time; this.curValue = this.lastNum; this.isPlayAni = true; } private PlayMoneyAniUpdate(dt) { if (this.isPlayAni) { this.curValue += this.delta * dt; if (this.curValue >= this.targetProgress) { this.isPlayAni = false; this.curValue = this.targetProgress; this.lastNum = this.targetProgress; } this.lbl_count.string = this.curValue.toFixed(this.toFixed); } } update(dt) { this.PlayMoneyAniUpdate(dt); } }