Countdown.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Learn cc.Class:
  2. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
  3. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
  4. // Learn Attribute:
  5. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
  6. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
  7. // Learn life-cycle callbacks:
  8. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
  9. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
  10. cc.Class({
  11. extends: cc.Component,
  12. properties: {
  13. // foo: {
  14. // // ATTRIBUTES:
  15. // default: null, // The default value will be used only when the component attaching
  16. // // to a node for the first time
  17. // type: cc.SpriteFrame, // optional, default is typeof default
  18. // serializable: true, // optional, default is true
  19. // },
  20. // bar: {
  21. // get () {
  22. // return this._bar;
  23. // },
  24. // set (value) {
  25. // this._bar = value;
  26. // }
  27. // },
  28. },
  29. // LIFE-CYCLE CALLBACKS:
  30. onLoad () {
  31. this.timeMax = 50;
  32. // this._init();
  33. timer = this;
  34. },
  35. //设置时间 并且开始
  36. setTimeInit(timeMax){
  37. this.timeMax = timeMax;
  38. this._init();
  39. },
  40. //增加 或者减少
  41. setTime(time){
  42. this.timer += time;
  43. },
  44. //开始倒计时 使用 timeMax 作为 倒计时的时间
  45. _init(){
  46. this.tag = false;
  47. this.timer = this.timeMax;
  48. this.t = 1;
  49. let self = this;
  50. this.slider = UtilsNode.getNode("Slider",this.node)
  51. this.time = UtilsNode.getNode("time",this.node)
  52. this.Alarmclock = UtilsNode.getNode("Alarmclock",this.time)
  53. this.t = UtilsNode.getNode("t",this.Alarmclock)
  54. this.mySlider = this.slider.getComponent("mySlider")
  55. this.timec = this.time.getComponent("time");
  56. this.mySlider.setCallBack(function (event) {
  57. var slider = event;
  58. // //do whatever you want with the slider
  59. //
  60. console.log("Slider3的回调函数",slider.progress);
  61. self.timec.setProgressBar(slider.progress);
  62. });
  63. this.sliderc =this.slider.getComponent(cc.Slider);
  64. },
  65. setTimeEndCallBack(timeEndCallBack){
  66. this.timeEndCallBack = timeEndCallBack;
  67. },
  68. timeEndCallBack(){},
  69. setTimeUpdateCallBack(timeUpdateCallBack){
  70. this.timeUpdateCallBack = timeUpdateCallBack;
  71. },
  72. timeUpdateCallBack(){},
  73. update (dt) {
  74. if (this.timer > 0) {
  75. this.timer-=dt;
  76. let tt = this.timer/this.timeMax;
  77. //设置倒计时的进度条
  78. this.timec.setProgressBar(tt);
  79. if (Math.floor(parseInt(this.timer)) < 10) {
  80. this.t.color = new cc.color(255,0,0,255)
  81. }
  82. this.t.getComponent(cc.Label).string = Math.floor(parseInt(this.timer))+"";
  83. if (this.timeUpdateCallBack!=null) {
  84. this.timeUpdateCallBack(this.timer);
  85. }
  86. }else{
  87. if (this.timec) {
  88. this.timec.setProgressBar(0);
  89. }
  90. if (this.timeEndCallBack != null) {
  91. this.timeEndCallBack();
  92. }
  93. }
  94. },
  95. });