BattleItemRedPacket.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { _decorator, Component, Label, Animation, utils, EventHandler } from 'cc';
  2. import { CountDown } from '../../core/ui/CountDown';
  3. import { Utils } from '../../core/utils/Utils';
  4. const { ccclass, property } = _decorator;
  5. /**
  6. * 切磋列表红包倒计时
  7. */
  8. @ccclass('BattleItemRedPacket')
  9. export class BattleItemRedPacket extends Component {
  10. @property({ type: Label, tooltip: "可领取文本" })
  11. getLabel: Label;
  12. @property({ type: Label, tooltip: "倒计时文本" })
  13. timeLabel: Label;
  14. @property({ type: Animation, tooltip: "红包特效" })
  15. animation: Animation;
  16. @property({ type: CountDown, tooltip: "倒计时组件" })
  17. redCountDown: CountDown;
  18. @property({ type: EventHandler, tooltip: "红包可领取" })
  19. public finishHandler: EventHandler;
  20. start() {
  21. }
  22. update() {
  23. }
  24. public setData(time: number) {
  25. if (time > Date.now()) {
  26. this.redCountDown.value = Math.ceil((time - Date.now()) / 1000);
  27. this.timeLabel.string = Utils.formatCountDown(this.redCountDown.value * 1000);
  28. this.redCountDown.play();
  29. this.getLabel.node.active = false;
  30. this.timeLabel.node.active = true;
  31. } else {
  32. this.getLabel.node.active = true;
  33. this.timeLabel.node.active = false;
  34. }
  35. }
  36. /**倒计时回调 */
  37. public onInterval(progress) {
  38. this.timeLabel.string = Utils.formatCountDown(progress * 1000);
  39. }
  40. /**倒计时结束回调 */
  41. public onComplete() {
  42. this.redCountDown.stop();
  43. this.animation && this.animation.play();
  44. this.getLabel.node.active = true;
  45. this.timeLabel.node.active = false;
  46. this.finishHandler.emit([]);
  47. }
  48. }