FCountDown.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * 战斗倒计时
  3. */
  4. const {ccclass, property} = cc._decorator;
  5. @ccclass
  6. export default class FCountDown extends cc.Component {
  7. @property(cc.Label)
  8. mTime: cc.Label = null;
  9. public isOver = true;
  10. /**
  11. * 开始的时间
  12. */
  13. private dtime = 0;
  14. private totalTime = 0;
  15. private callback:()=>void;
  16. onLoad(){
  17. this.node.active = false;
  18. }
  19. public setCallback(callback:()=>void){
  20. this.callback = callback;
  21. }
  22. public startCountDown(totalTime:number){
  23. this.node.active = true;
  24. this.totalTime = totalTime;
  25. this.dtime = new Date().getTime();
  26. this.isOver = false;
  27. }
  28. public stopCountDown(){
  29. this.node.active = false;
  30. this.isOver = true;
  31. }
  32. /**
  33. * 暂停住倒计时
  34. */
  35. public pauseTime(){
  36. }
  37. update (dt) {
  38. if(this.isOver){
  39. return;
  40. }
  41. let nowTime = new Date().getTime();
  42. let rest = this.totalTime - Math.floor((nowTime - this.dtime)/1000);
  43. if(rest < 0){
  44. this.isOver = true;
  45. this.callback();
  46. }else{
  47. this.mTime.string = ''+rest;
  48. }
  49. }
  50. }