NumberAnim.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. const { ccclass, property } = cc._decorator;
  2. /**
  3. * 数字跳动动画
  4. * @author 薛鸿潇 kaka
  5. */
  6. @ccclass
  7. export default class NumberAnim extends cc.Component {
  8. /** 动画目标 */
  9. private lbl_count: cc.Label = null;
  10. onLoad() {
  11. this.lbl_count = this.node.getComponent(cc.Label) || this.node.addComponent(cc.Label);
  12. }
  13. /**
  14. * 设置数值
  15. * @param new_value 目标值
  16. */
  17. public setValue(new_value: number, toFixed: number = 0) {
  18. if (this.targetProgress == new_value) return;
  19. this.toFixed = toFixed;
  20. this.targetProgress = new_value;
  21. this.PlayMoneyAni(0.3);
  22. }
  23. //////////////////////////////////数字滚动效果//////////////////////////////////
  24. /** 滚动的目标值 */
  25. private targetProgress: number = 0;
  26. private delta: number = 0;
  27. /** 滚动中的数字 */
  28. private curValue: number = 0;
  29. /** 上次值 */
  30. private lastNum: number = 0;
  31. /** 动画中 */
  32. private isPlayAni: boolean = false;
  33. /** 保留小数点位数 */
  34. private toFixed: number = 0;
  35. private PlayMoneyAni(time: number) {
  36. this.delta = (this.targetProgress - this.lastNum);// / time;
  37. this.curValue = this.lastNum;
  38. this.isPlayAni = true;
  39. }
  40. private PlayMoneyAniUpdate(dt) {
  41. if (this.isPlayAni) {
  42. this.curValue += this.delta * dt;
  43. if (this.curValue >= this.targetProgress) {
  44. this.isPlayAni = false;
  45. this.curValue = this.targetProgress;
  46. this.lastNum = this.targetProgress;
  47. }
  48. this.lbl_count.string = this.curValue.toFixed(this.toFixed);
  49. }
  50. }
  51. update(dt) {
  52. this.PlayMoneyAniUpdate(dt);
  53. }
  54. }