CocosCreator源碼分析-main.js

定義的全局方法 boot

window.boot =?function?() {

var?settings = window._CCSettings;

window._CCSettings =?undefined;

var?onProgress =?null;

//通過解構(gòu) 聲明四個(gè)變量,分別為bundle?的名字,都是內(nèi)置bundle

let?{ RESOURCES, INTERNAL, MAIN, START_SCENE } = cc.AssetManager.BuiltinBundleName;

//聲明一個(gè)方法,控制加載進(jìn)度條,

function?setLoadingDisplay () {

// Loading splash scene

var?splash = document.getElementById('splash');

var?progressBar = splash.querySelector('.progress-bar span');

onProgress =?function?(finish, total) {

var?percent =?100?* finish / total;

if?(progressBar) {

progressBar.style.width = percent.toFixed(2) +?'%';

}

};

splash.style.display =?'block';

progressBar.style.width =?'0%';

cc.director.once(cc.Director.EVENT_AFTER_SCENE_LAUNCH,?function?() {

splash.style.display =?'none';//加載完畢后隱藏

});

}

定義onStart

//在run起游戲后?調(diào)用onstart

var?onStart =?function?() {

//對(duì)于 Apple 這種支持 Retina 顯示的設(shè)備上默認(rèn)進(jìn)行優(yōu)化,而其他類型設(shè)備默認(rèn)不進(jìn)行優(yōu)化,

cc.view.enableRetina(true);

//設(shè)置當(dāng)發(fā)現(xiàn)瀏覽器的尺寸改變時(shí),是否自動(dòng)調(diào)整 canvas 尺寸大小。

cc.view.resizeWithBrowserSize(true);

if?(cc.sys.isBrowser) {

setLoadingDisplay();//瀏覽器的時(shí)候處理進(jìn)度條

}

if?(cc.sys.isMobile) {

if?(settings.orientation ===?'landscape') {//橫屏

cc.view.setOrientation(cc.macro.ORIENTATION_LANDSCAPE);

}

else?if?(settings.orientation ===?'portrait') {//豎屏

cc.view.setOrientation(cc.macro.ORIENTATION_PORTRAIT);

}

// 下面幾個(gè)平臺(tái)移動(dòng)端游戲會(huì)在移動(dòng)端自動(dòng)嘗試進(jìn)入全屏模式。

cc.view.enableAutoFullScreen([

cc.sys.BROWSER_TYPE_BAIDU,

cc.sys.BROWSER_TYPE_BAIDU_APP,

cc.sys.BROWSER_TYPE_WECHAT,

cc.sys.BROWSER_TYPE_MOBILE_QQ,

cc.sys.BROWSER_TYPE_MIUI,

].indexOf(cc.sys.browserType) <?0);

}//

// Limit downloading max concurrent task to 2,

// more tasks simultaneously may cause performance draw back on some android system / browsers.

// You can adjust the number based on your own test result, you have to set it before any loading process to take effect.

if?(cc.sys.isBrowser && cc.sys.os === cc.sys.OS_ANDROID) {

//下載時(shí)的最大并發(fā)數(shù)

cc.assetManager.downloader.maxConcurrency =?2;

//下載時(shí)每幀可以啟動(dòng)的最大請(qǐng)求數(shù)

cc.assetManager.downloader.maxRequestsPerFrame =?2;

}

var?launchScene = settings.launchScene;

var?bundle = cc.assetManager.bundles.find(function?(b) {

return?b.getSceneInfo(launchScene);

});

//通過場(chǎng)景名稱加載分包中的場(chǎng)景。

bundle.loadScene(launchScene,?null, onProgress,

function?(err, scene) {

if?(!err) {

cc.director.runSceneImmediate(scene);

if?(cc.sys.isBrowser) {

// show canvas

var?canvas = document.getElementById('GameCanvas');

canvas.style.visibility =?'';

var?div = document.getElementById('GameDiv');

if?(div) {

div.style.backgroundImage =?'';

}

console.log('Success to load scene: '?+ launchScene);

}

}

}

);

};

//游戲運(yùn)行的各項(xiàng)參數(shù)

var?option = {

id:?'GameCanvas',

debugMode: settings.debug ? cc.debug.DebugMode.INFO : cc.debug.DebugMode.ERROR,//幾種模式?具體下面詳細(xì)說明

showFPS: settings.debug,//

frameRate:?60,

groupList: settings.groupList,?分組

collisionMatrix: settings.collisionMatrix,?分組可進(jìn)行碰撞的配置

};

1. debugMode(debug 模式,但是在瀏覽器中這個(gè)選項(xiàng)會(huì)被忽略) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?<br/>

? ? * ? ? ?"debugMode" 各種設(shè)置選項(xiàng)的意義。? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <br/>

? ? * ? ? ? ? ?0 - 沒有消息被打印出來。? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <br/>

? ? * ? ? ? ? ?1 - cc.error,cc.assert,cc.warn,cc.log 將打印在 console 中。? ? ? ? ? ? ? ? ?<br/>

? ? * ? ? ? ? ?2 - cc.error,cc.assert,cc.warn 將打印在 console 中。? ? ? ? ? ? ? ? ? ? ? ? ?<br/>

? ? * ? ? ? ? ?3 - cc.error,cc.assert 將打印在 console 中。? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <br/>

? ? * ? ? ? ? ?4 - cc.error,cc.assert,cc.warn,cc.log 將打印在 canvas 中(僅適用于 web 端)。<br/>

? ? * ? ? ? ? ?5 - cc.error,cc.assert,cc.warn 將打印在 canvas 中(僅適用于 web 端)。? ? ? ? <br/>

? ? * ? ? ? ? ?6 - cc.error,cc.assert 將打印在 canvas 中(僅適用于 web 端)。? ? ? ? ? ? ? ? ?<br/>

? ? * 2. showFPS(顯示 FPS) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?<br/>

? ? * ? ? ?當(dāng) showFPS 為 true 的時(shí)候界面的左下角將顯示 fps 的信息,否則被隱藏。?

?* 4. frameRate (幀率) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?<br/>

? ? * ? ? ?“frameRate” 設(shè)置想要的幀率你的游戲,但真正的FPS取決于你的游戲?qū)崿F(xiàn)和運(yùn)行環(huán)境。? ? ?<br/>

根據(jù)配置初始化?assetManager

cc.assetManager.init({

bundleVers: settings.bundleVers,

remoteBundles: settings.remoteBundles,

server: settings.server

});

//聲明容器,存放內(nèi)置bundle

let?bundleRoot = [INTERNAL, MAIN];

settings.hasStartSceneBundle && bundleRoot.push(START_SCENE);

settings.hasResourcesBundle && bundleRoot.push(RESOURCES);

var?count =?0;

function?cb (err) {

if?(err)?return?console.error(err.message, err.stack);

count++;

//第三步,內(nèi)置bundle 加載完畢,

運(yùn)行游戲,并且指定引擎配置和 onStart 的回調(diào)。

if?(count === bundleRoot.length +?1) {

cc.game.run(option, onStart);

}

}

//第一步加載導(dǎo)入為插件的script

cc.assetManager.loadScript(settings.jsList.map(function?(x) {?return?'src/'?+ x;}), cb);

第二步,加載內(nèi)置的bundle

for?(let?i =?0; i < bundleRoot.length; i++) {

cc.assetManager.loadBundle(bundleRoot[i], cb);

}

};

if?(window.jsb) {

var?isRuntime = (typeof?loadRuntime ===?'function');

if?(isRuntime) {

require('src/settings.js');

require('src/cocos2d-runtime.js');

if?(CC_PHYSICS_BUILTIN || CC_PHYSICS_CANNON) {

require('src/physics.js');

}

require('jsb-adapter/engine/index.js');

}

else?{

require('src/settings.js');

require('src/cocos2d-jsb.js');

if?(CC_PHYSICS_BUILTIN || CC_PHYSICS_CANNON) {

require('src/physics.js');

}

require('jsb-adapter/jsb-engine.js');

}

/*

* 是否在將貼圖上傳至 GPU 之后刪除原始圖片緩存,刪除之后圖片將無法進(jìn)行 [動(dòng)態(tài)合圖](https://docs.cocos.com/creator/manual/zh/advanced-topics/dynamic-atlas.html)。

? ? * 在 Web 平臺(tái),你通常不需要開啟這個(gè)選項(xiàng),因?yàn)樵?Web 平臺(tái) Image 對(duì)象所占用的內(nèi)存很小。

? ? * 但是在微信小游戲平臺(tái)的當(dāng)前版本,Image 對(duì)象會(huì)緩存解碼后的圖片數(shù)據(jù),它所占用的內(nèi)存空間很大。

? ? * 所以我們?cè)谖⑿牌脚_(tái)默認(rèn)開啟了這個(gè)選項(xiàng),這樣我們就可以在上傳 GL 貼圖之后立即釋放 Image 對(duì)象的內(nèi)存,避免過高的內(nèi)存占用。

*/

cc.macro.CLEANUP_IMAGE_CACHE =?true;

window.boot();

}

歡迎交流

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容