loadingmanager.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. window.loadmanager = {}
  11. cc.Class({
  12. extends: cc.Component,
  13. properties: {
  14. // foo: {
  15. // // ATTRIBUTES:
  16. // default: null, // The default value will be used only when the component attaching
  17. // // to a node for the first time
  18. // type: cc.SpriteFrame, // optional, default is typeof default
  19. // serializable: true, // optional, default is true
  20. // },
  21. // bar: {
  22. // get () {
  23. // return this._bar;
  24. // },
  25. // set (value) {
  26. // this._bar = value;
  27. // }
  28. // },
  29. },
  30. // LIFE-CYCLE CALLBACKS:
  31. // onLoad () {},
  32. start() {
  33. loadmanager = this
  34. //常驻节点
  35. cc.game.addPersistRootNode(this.node);
  36. this.initScene()
  37. //初始化加载进度监听
  38. this.initLoadListener()
  39. this.loading = UtilsNode.getNode("loading",this.node)
  40. this.bg = UtilsNode.getNode("bg",this.node)
  41. },
  42. initLoadListener() {
  43. // this.progressTag = false;
  44. // cc.loader.onProgress = function (completeCount, totalCount) {
  45. // if (this.progressTag) {
  46. // this.progress = (1 * completeCount / totalCount).toFixed(1);
  47. // console.log("进度条",this.progress + '%');
  48. // // if (this.progressNode != null) {
  49. // // this.progressNode.getComponent(cc.ProgressBar).progress = this.progress;
  50. // // }
  51. //
  52. // }
  53. //
  54. // }.bind(this);
  55. },
  56. initScene() {
  57. this.scene = ["config", "game"]
  58. },
  59. showLoading(b) {
  60. this.loading.active = b
  61. this.bg.active = b
  62. },
  63. goto(index,b) {
  64. this.showLoading(true);
  65. // utils.toLoadScene("config",function () {
  66. // this.progressTag = true;
  67. // }.bind(this))
  68. var scene = this.scene[index]
  69. this.loading.getComponent(cc.ProgressBar).progress= 0
  70. cc.director.preloadScene(scene, (completedCount, totalCount, item) => {
  71. cc.log('加载进度显示');
  72. cc.log("completedCount = " + completedCount + ",totalCount=" + totalCount);
  73. cc.log('百分比:' + Math.floor(completedCount / totalCount * 100));
  74. this.loading.getComponent(cc.ProgressBar).progress = completedCount / totalCount;
  75. if (totalCount - completedCount == 0) {
  76. // if (b) {
  77. // this.loading.active = false
  78. // utils.toLoadScene(scene)
  79. // }
  80. this.showLoading(false);
  81. utils.toLoadScene(scene)
  82. }
  83. }, (error) => {
  84. console.log(error);
  85. });
  86. }
  87. // update (dt) {},
  88. });