LoadingCtrl.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * 资源加载控制器
  3. */
  4. cc.Class({
  5. extends: cc.Component,
  6. properties: {
  7. loadPrecentLabel: cc.Label
  8. },
  9. onLoad () {
  10. this.percent = 0;
  11. },
  12. backKeyOnClicked () {
  13. return this.node.active;
  14. },
  15. showLoadingAlert () {
  16. this.percent = 0;
  17. this.loadPrecentLabel.string = '已加载' + this.percent + '%';
  18. this.node.active = true;
  19. // 避免每个场景更新一遍ctrl
  20. // 这里直接通过getChildByName获取子节点
  21. let bg = this.node.getChildByName('bg');
  22. let icon = this.node.getChildByName('icon');
  23. let precent = this.node.getChildByName('precent');
  24. if (bg) {
  25. bg.active = false;
  26. }
  27. if (icon) {
  28. icon.active = false;
  29. }
  30. if (precent) {
  31. precent.active = false;
  32. }
  33. this.scheduleOnce(() => {
  34. if (bg) {
  35. bg.active = true;
  36. }
  37. if (icon) {
  38. icon.active = true;
  39. }
  40. if (precent) {
  41. precent.active = true;
  42. }
  43. }, 1);
  44. },
  45. updateLoadingProgress (completedCount, totalCount, item) {
  46. if (completedCount == totalCount) {
  47. this.node.active = false;
  48. this.percent = 0;
  49. return;
  50. }
  51. // 显示加载进度
  52. // totalCount 会在加载过程中增加导致总进度变小
  53. // 所以需要比较之前缓存进度大小,避免进度后退的情况
  54. let percent = Math.floor(completedCount / totalCount * 100);
  55. if (percent > this.percent) {
  56. this.percent = percent;
  57. }
  58. this.loadPrecentLabel.string = '已加载' + this.percent + '%';
  59. },
  60. /**
  61. * 显示提示
  62. *
  63. * @author libo
  64. * @date 2019-07-04
  65. * @param {*} tips
  66. */
  67. showTips (tips, duration, cb) {
  68. // 显示tips
  69. this.node.active = true;
  70. this.loadPrecentLabel.string = tips;
  71. if (G.FuncUtils.isNumber(duration) && duration > 0) {
  72. cc.tween(this.node)
  73. .delay(duration)
  74. .call(() => {
  75. this.hideTips();
  76. if (cb) {
  77. cb();
  78. }
  79. })
  80. .start();
  81. }
  82. },
  83. /**
  84. * 隐藏
  85. *
  86. * @author libo
  87. * @date 2019-07-04
  88. */
  89. hideTips () {
  90. // 隐藏tips
  91. this.node.active = false;
  92. this.node.stopAllActions();
  93. }
  94. });