LabelAnim.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { gameMethod } from "../../../common/gameMethod";
  2. import GameMath from "../../../utils/GameMath";
  3. const { ccclass, property, menu } = cc._decorator;
  4. @ccclass
  5. @menu('Label/LabelAnim')
  6. export default class LabelAnim extends cc.Component {
  7. curVal = { val: 0 }
  8. showAnim: boolean = false
  9. type: number = 0// 0 完整 1缩写
  10. realStr: string = ""
  11. backupValue: number = 0 // 备份的数据,当多次触发时,第二次触发时的初始值直接变成第一次的最终值
  12. start() {
  13. //this.setLabelByType()
  14. }
  15. onDisable() {
  16. cc.Tween.stopAllByTarget(this.curVal)
  17. }
  18. update(dt) {
  19. if (!this.showAnim) { return }
  20. this.setLabelByType()
  21. }
  22. //
  23. initLabel(val: number, type: number = 0, realStr: string = "") {
  24. this.realStr = ""
  25. this.curVal = { val: val }
  26. this.type = type
  27. this.setLabelByType()
  28. }
  29. setLabel(val: number, time: number = 0.5, type: number = 0, realStr: string = "",) {
  30. if (this.showAnim) {
  31. cc.Tween.stopAllByTarget(this.curVal.val)
  32. this.curVal.val = this.backupValue
  33. }
  34. this.backupValue = val
  35. this.showAnim = true
  36. this.type = type
  37. this.realStr = realStr
  38. cc.tween(this.curVal).to(time, { val: val }).
  39. call(() => {
  40. this.curVal = { val: val }
  41. this.showAnim = false
  42. this.setLabelByType()
  43. }).
  44. start()
  45. }
  46. private setLabelByType() {
  47. if (this.node == null || this.node.isValid == false) { return }
  48. let str = Math.round(this.curVal.val)
  49. if (gameMethod.isEmpty(this.node) || gameMethod.isEmpty(this.node.getComponent(cc.Label))) return
  50. switch (this.type) {
  51. case 0:
  52. if (this.realStr.length > 0) {
  53. this.node.getComponent(cc.Label).string = this.realStr.replace("%{0}", str.toString())
  54. } else {
  55. this.node.getComponent(cc.Label).string = str.toString()
  56. }
  57. break
  58. case 1:
  59. if (this.realStr.length > 0) {
  60. this.node.getComponent(cc.Label).string = this.realStr.replace("%{0}", GameMath.showNum(str, 100000000))
  61. } else {
  62. this.node.getComponent(cc.Label).string = GameMath.showNum(str, 100000000)
  63. }
  64. break
  65. }
  66. }
  67. }