count_down.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { _decorator, Component, Label, Node, ProgressBar } from 'cc';
  2. import { att_count_down } from '../../../data/data';
  3. import { gameManager } from '../gameManager';
  4. const { ccclass, property } = _decorator;
  5. @ccclass('count_down')
  6. export class count_down extends Component {
  7. private m_data:att_count_down = null;
  8. @property(Node) ProgressBar_Bg:Node = null;
  9. @property(Node) ProgressBar_Bar:Node = null;
  10. @property(Node) img_text:Node = null;
  11. @property(Node) lab_time:Node = null;
  12. private time_count:number = 0;
  13. private m_over_call = null;
  14. public init(data:att_count_down,on_over_call){
  15. this.m_data = data;
  16. this.m_over_call = on_over_call;
  17. gameManager.initUiBaseAtt(this.ProgressBar_Bar,this.m_data.ProgressBar_Bar)
  18. gameManager.initUiBaseAtt(this.ProgressBar_Bg,this.m_data.ProgressBar_Bg)
  19. gameManager.initUiBaseAtt(this.img_text,this.m_data.img_text)
  20. this.ProgressBar_Bg.getComponent(ProgressBar).totalLength = this.m_data.ProgressBar_Bg.width;
  21. if(this.m_data.time_count>0){
  22. if(this.m_data.is_show_time){
  23. this.lab_time.getComponent(Label).string = `${this.m_data.time_count}秒`
  24. this.lab_time.active = true;
  25. }else{
  26. this.lab_time.active = false;
  27. }
  28. }else{
  29. this.lab_time.active = false;
  30. }
  31. }
  32. public getData(){
  33. return this.m_data
  34. }
  35. public startCountDown(){
  36. this.schedule(()=>{
  37. this.time_count+=1;
  38. if(this.time_count>this.m_data.time_count){
  39. this.unscheduleAllCallbacks()
  40. if(this.m_over_call!=null){
  41. this.m_over_call(true)
  42. }
  43. return
  44. }
  45. this.ProgressBar_Bg.getComponent(ProgressBar).progress = 1-this.time_count/this.m_data.time_count
  46. if(this.m_data.is_show_time){
  47. this.lab_time.getComponent(Label).string = `${this.m_data.time_count-this.time_count}秒`
  48. }
  49. },1)
  50. }
  51. }