CCLoaderScene.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /****************************************************************************
  2. Copyright (c) 2011-2012 cocos2d-x.org
  3. Copyright (c) 2013-2014 Chukong Technologies Inc.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. /**
  22. * <p>cc.LoaderScene is a scene that you can load it when you loading files</p>
  23. * <p>cc.LoaderScene can present thedownload progress </p>
  24. * @class
  25. * @extends cc.Scene
  26. * @example
  27. * var lc = new cc.LoaderScene();
  28. */
  29. cc.LoaderScene = cc.Scene.extend({
  30. _interval : null,
  31. _label : null,
  32. _className:"LoaderScene",
  33. /**
  34. * Contructor of cc.LoaderScene
  35. * @returns {boolean}
  36. */
  37. init : function(){
  38. var self = this;
  39. //logo
  40. var logoWidth = 160;
  41. var logoHeight = 200;
  42. // bg
  43. var bgLayer = self._bgLayer = new cc.LayerColor(cc.color(32, 32, 32, 255));
  44. bgLayer.setPosition(cc.visibleRect.bottomLeft);
  45. self.addChild(bgLayer, 0);
  46. //image move to CCSceneFile.js
  47. var fontSize = 24, lblHeight = -logoHeight / 2 + 100;
  48. /*
  49. if(cc._loaderImage){
  50. //loading logo
  51. cc.loader.loadImg(cc._loaderImage, {isCrossOrigin : false }, function(err, img){
  52. logoWidth = img.width;
  53. logoHeight = img.height;
  54. self._initStage(img, cc.visibleRect.center);
  55. });
  56. fontSize = 14;
  57. lblHeight = -logoHeight / 2 - 10;
  58. }*/
  59. //loading percent
  60. var label = self._label = cc.LabelTTF.create("Loading... 0%", "Arial", fontSize);
  61. label.setPosition(cc.pAdd(cc.visibleRect.center, cc.p(0, lblHeight)));
  62. label.setColor(cc.color(180, 180, 180));
  63. bgLayer.addChild(this._label, 10);
  64. return true;
  65. },
  66. _initStage: function (img, centerPos) {
  67. var self = this;
  68. var texture2d = self._texture2d = new cc.Texture2D();
  69. texture2d.initWithElement(img);
  70. texture2d.handleLoadedTexture();
  71. var logo = self._logo = cc.Sprite.create(texture2d);
  72. logo.setScale(cc.contentScaleFactor());
  73. logo.x = centerPos.x;
  74. logo.y = centerPos.y;
  75. self._bgLayer.addChild(logo, 10);
  76. },
  77. /**
  78. * custom onEnter
  79. */
  80. onEnter: function () {
  81. var self = this;
  82. cc.Node.prototype.onEnter.call(self);
  83. self.schedule(self._startLoading, 0.3);
  84. },
  85. /**
  86. * custom onExit
  87. */
  88. onExit: function () {
  89. cc.Node.prototype.onExit.call(this);
  90. var tmpStr = "Loading... 0%";
  91. this._label.setString(tmpStr);
  92. },
  93. /**
  94. * init with resources
  95. * @param {Array} resources
  96. * @param {Function|String} cb
  97. */
  98. initWithResources: function (resources, cb) {
  99. if(typeof resources == "string") resources = [resources];
  100. this.resources = resources || [];
  101. this.cb = cb;
  102. },
  103. _startLoading: function () {
  104. var self = this;
  105. self.unschedule(self._startLoading);
  106. var res = self.resources;
  107. cc.loader.load(res,
  108. function (result, count, loadedCount) {
  109. var percent = (loadedCount / count * 100) | 0;
  110. percent = Math.min(percent, 100);
  111. self._label.setString("Loading... " + percent + "%");
  112. }, function () {
  113. if (self.cb)
  114. self.cb();
  115. });
  116. }
  117. });
  118. /**
  119. * <p>cc.LoaderScene.preload can present a loaderScene with download progress.</p>
  120. * <p>when all the resource are downloaded it will invoke call function</p>
  121. * @param resources
  122. * @param cb
  123. * @returns {cc.LoaderScene|*}
  124. * @example
  125. * //Example
  126. * cc.LoaderScene.preload(g_resources, function () {
  127. cc.director.runScene(new HelloWorldScene());
  128. }, this);
  129. */
  130. cc.LoaderScene.preload = function(resources, cb){
  131. var _cc = cc;
  132. if(!_cc.loaderScene) {
  133. _cc.loaderScene = new cc.LoaderScene();
  134. _cc.loaderScene.init();
  135. }
  136. _cc.loaderScene.initWithResources(resources, cb);
  137. cc.director.runScene(_cc.loaderScene);
  138. return _cc.loaderScene;
  139. };