main.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. window.boot = function () {
  2. var settings = window._CCSettings;
  3. window._CCSettings = undefined;
  4. var onProgress = null;
  5. var RESOURCES = cc.AssetManager.BuiltinBundleName.RESOURCES;
  6. var INTERNAL = cc.AssetManager.BuiltinBundleName.INTERNAL;
  7. var MAIN = cc.AssetManager.BuiltinBundleName.MAIN;
  8. function setLoadingDisplay () {
  9. // Loading splash scene
  10. var splash = document.getElementById('splash');
  11. var progressBar = splash.querySelector('.progress-bar-color');
  12. onProgress = function (finish, total) {
  13. var percent = 100 * finish / total;
  14. percent = percent < 5 ? 5 : percent;
  15. if (progressBar) {
  16. progressBar.style.width = percent.toFixed(2) + '%';
  17. }
  18. };
  19. splash.style.display = 'block';
  20. progressBar.style.width = '0%';
  21. cc.director.once(cc.Director.EVENT_AFTER_SCENE_LAUNCH, function () {
  22. splash.style.display = 'none';
  23. });
  24. }
  25. var onStart = function () {
  26. cc.view.enableRetina(true);
  27. cc.view.resizeWithBrowserSize(true);
  28. if (cc.sys.isBrowser) {
  29. setLoadingDisplay();
  30. }
  31. if (cc.sys.isMobile) {
  32. if (settings.orientation === 'landscape') {
  33. cc.view.setOrientation(cc.macro.ORIENTATION_LANDSCAPE);
  34. }
  35. else if (settings.orientation === 'portrait') {
  36. cc.view.setOrientation(cc.macro.ORIENTATION_PORTRAIT);
  37. }
  38. cc.view.enableAutoFullScreen([
  39. cc.sys.BROWSER_TYPE_BAIDU,
  40. cc.sys.BROWSER_TYPE_BAIDU_APP,
  41. cc.sys.BROWSER_TYPE_WECHAT,
  42. cc.sys.BROWSER_TYPE_MOBILE_QQ,
  43. cc.sys.BROWSER_TYPE_MIUI,
  44. cc.sys.BROWSER_TYPE_HUAWEI,
  45. cc.sys.BROWSER_TYPE_UC,
  46. ].indexOf(cc.sys.browserType) < 0);
  47. }
  48. // Limit downloading max concurrent task to 2,
  49. // more tasks simultaneously may cause performance draw back on some android system / browsers.
  50. // You can adjust the number based on your own test result, you have to set it before any loading process to take effect.
  51. if (cc.sys.isBrowser && cc.sys.os === cc.sys.OS_ANDROID) {
  52. cc.assetManager.downloader.maxConcurrency = 2;
  53. cc.assetManager.downloader.maxRequestsPerFrame = 2;
  54. }
  55. var launchScene = settings.launchScene;
  56. var bundle = cc.assetManager.bundles.find(function (b) {
  57. return b.getSceneInfo(launchScene);
  58. });
  59. bundle.loadScene(launchScene, null, onProgress,
  60. function (err, scene) {
  61. if (!err) {
  62. cc.director.runSceneImmediate(scene);
  63. if (cc.sys.isBrowser) {
  64. // show canvas
  65. var canvas = document.getElementById('GameCanvas');
  66. canvas.style.visibility = '';
  67. var div = document.getElementById('GameDiv');
  68. if (div) {
  69. div.style.backgroundImage = '';
  70. }
  71. console.log('Success to load scene: ' + launchScene);
  72. }
  73. }
  74. }
  75. );
  76. };
  77. var option = {
  78. id: 'GameCanvas',
  79. debugMode: settings.debug ? cc.debug.DebugMode.INFO : cc.debug.DebugMode.ERROR,
  80. showFPS: settings.debug,
  81. frameRate: 60,
  82. groupList: settings.groupList,
  83. collisionMatrix: settings.collisionMatrix,
  84. };
  85. cc.assetManager.init({
  86. bundleVers: settings.bundleVers,
  87. remoteBundles: settings.remoteBundles,
  88. server: settings.server
  89. });
  90. var bundleRoot = [INTERNAL];
  91. settings.hasResourcesBundle && bundleRoot.push(RESOURCES);
  92. var count = 0;
  93. function cb (err) {
  94. if (err) return console.error(err.message, err.stack);
  95. count++;
  96. if (count === bundleRoot.length + 1) {
  97. cc.assetManager.loadBundle(MAIN, function (err) {
  98. if (!err) cc.game.run(option, onStart);
  99. });
  100. }
  101. }
  102. cc.assetManager.loadScript(settings.jsList.map(function (x) { return 'src/' + x;}), cb);
  103. for (var i = 0; i < bundleRoot.length; i++) {
  104. cc.assetManager.loadBundle(bundleRoot[i], cb);
  105. }
  106. };
  107. if (window.jsb) {
  108. var isRuntime = (typeof loadRuntime === 'function');
  109. if (isRuntime) {
  110. require('src/settings.js');
  111. require('src/cocos2d-runtime.js');
  112. if (CC_PHYSICS_BUILTIN || CC_PHYSICS_CANNON) {
  113. require('src/physics.js');
  114. }
  115. require('jsb-adapter/engine/index.js');
  116. }
  117. else {
  118. require('src/settings.js');
  119. require('src/cocos2d-jsb.js');
  120. if (CC_PHYSICS_BUILTIN || CC_PHYSICS_CANNON) {
  121. require('src/physics.js');
  122. }
  123. require('jsb-adapter/jsb-engine.js');
  124. }
  125. cc.macro.CLEANUP_IMAGE_CACHE = true;
  126. window.boot();
  127. }