ProgressBar.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. const { ccclass, property } = cc._decorator;
  2. @ccclass
  3. export default class ProgressBar extends cc.Component {
  4. @property({ type: cc.Sprite, displayName: '进度条背景' })
  5. blood_1: cc.Sprite = null;
  6. @property({ type: cc.Sprite, displayName: '进度条动画节点' })
  7. blood_2: cc.Sprite = null;
  8. @property(cc.Label)
  9. lbl_progress: cc.Label = null;
  10. private maxlen: number;
  11. private current: number;
  12. private max: number;
  13. private tweenSpeed: number = 100;
  14. onLoad() {
  15. this.maxlen = this.blood_1.node.width;
  16. }
  17. init(current, max) {
  18. this.current = current;
  19. this.max = max;
  20. this.blood_2.node.width = this.maxlen;
  21. this.updateLabel();
  22. }
  23. setProgress(current, tween: boolean = false) {
  24. let dis = (this.current - current) * this.maxlen / this.max;
  25. this.current = current;
  26. let w = current / this.max * this.maxlen;
  27. if (tween) {
  28. let t = dis / this.tweenSpeed;
  29. cc.tween(this.blood_2.node).to(t, { width: w }).start();
  30. }
  31. else {
  32. this.blood_2.node.width = w;
  33. }
  34. this.updateLabel();
  35. }
  36. updateLabel() {
  37. if (this.lbl_progress) {
  38. this.lbl_progress.string = Math.floor(this.current) + "/" + Math.floor(this.max);
  39. }
  40. }
  41. }