main.js 4.9 KB

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