参考cocos论坛:http://forum.cocos.com/t/1-5-2-demo/48200
demo: https://github.com/zhangjiangyi/HallAndChild
主要的是实现下载子游戏、跳转到子游戏,然后由子游戏返回到大厅。
我们知道、在启动cocos工程的时候,首先要加载main.js文件,加载准备工作,执行一些配置操作。而由大厅跳转到子游戏的话我们是否需要知道子游戏的配置文件setting.js跟main.js。然后由大厅跳转到子游戏的话其实就是获取子游戏的main.js文件就行了。
首先就是要用热更新的方式下载子游戏。但是热更新下载子游戏的话需要project.manifest文件跟远程文件做对比,但是一开始的时候是子游戏是空空如也的,那就只能用远程的方式获取project.manifest文件,然后进行热更新了。
checkUpdate: function () {
let UIRLFILE = "http://192.168.92.59/update/remote-assets";
let remoteManifestUrl = this._storagePath + "/project.manifest";
this.manifestUrl = remoteManifestUrl;
let customManifestStr = JSON.stringify({
"packageUrl": UIRLFILE,
"remoteManifestUrl": UIRLFILE + "/project.manifest",
"remoteVersionUrl": UIRLFILE + "/version.manifest",
"version": "1.0.0.0",
"assets": {},
"searchPaths": []
});
this._checkListener = new jsb.EventListenerAssetsManager(this._am, this.checkCb.bind(this));
cc.eventManager.addListener(this._checkListener, 1);
if (this._am.getState() === jsb.AssetsManager.State.UNINITED) {
if (jsb.fileUtils.isFileExist(remoteManifestUrl)) {
console.log('加载本地Manifest');
this._am.loadLocalManifest(this.manifestUrl);
} else {
console.log('加载网络Manifest');
let manifest = new jsb.Manifest(customManifestStr, this._storagePath);
this._am.loadLocalManifest(manifest, this._storagePath);
}
}
this.prmopt.string = '正在检查版本信息';
this._am.checkUpdate();
},
大厅的主要逻辑HotUpdate代码如下(主要负责下载子游戏,并跳转到子游戏):
cc.Class({
extends: cc.Component,
properties: {
_am: null,
_updating: false,
_canRetry: true,
_storagePath: '',
_version: -1,
},
// use this for initialization
onLoad: function () {
this.initView();
this.initAssetsManage();
},
initView() {
this.percentLabel = cc.find('Canvas/percent').getComponent(cc.Label);
this.prmopt = cc.find('Canvas/prompt').getComponent(cc.Label);
},
initAssetsManage() {
if (cc.sys.isBrowser) {
return;
}
this._storagePath = ((jsb.fileUtils ? jsb.fileUtils.getWritablePath() : '/') + 'ALLGame/subgame');
console.log('SubGame path ' + this._storagePath);
let versionCompareHandle = function (versionA, versionB) {
let vA = versionA.split('.');
let vB = versionB.split('.');
for (let i = 0; i < vA.length; ++i) {
let a = parseInt(vA[i]);
let b = parseInt(vB[i] || 0);
if (a !== b) {
return a - b;
}
}
return vB.length > vA.length ? -1 : 0;
};
this._am = new jsb.AssetsManager('', this._storagePath, versionCompareHandle);
if (!cc.sys.ENABLE_GC_FOR_NATIVE_OBJECTS) {
this._am.retain();
}
if (cc.sys.os === cc.sys.OS_ANDROID) {
this._am.setMaxConcurrentTask(2);
}
},
checkCb: function (event) {
let delayTime = 2000;
switch (event.getEventCode()) {
case jsb.EventAssetsManager.ERROR_NO_LOCAL_MANIFEST:
this.prmopt.string = "本地Manifest文件未找到";
break;
case jsb.EventAssetsManager.ERROR_DOWNLOAD_MANIFEST:
case jsb.EventAssetsManager.ERROR_PARSE_MANIFEST:
this.prmopt.string = "加载Manifest文件失败";
break;
case jsb.EventAssetsManager.ALREADY_UP_TO_DATE:
this.prmopt.string = "已经是最新版本";
delayTime = 1000;
break;
case jsb.EventAssetsManager.NEW_VERSION_FOUND:
this.prmopt.string = '找到新版本';
cc.eventManager.removeListener(this._checkListener);
this._checkListener = null;
this._updating = false;
setTimeout(() => this.hotUpdate(), 1000);
return;
default:
return;
}
cc.eventManager.removeListener(this._checkListener);
this._checkListener = null;
this._updating = false;
},
updateCb: function (event) {
console.log('update eventCode = ' + event.getEventCode(), 'update msg = ' + event.getMessage());
switch (event.getEventCode()) {
case jsb.EventAssetsManager.ERROR_NO_LOCAL_MANIFEST:
this.prmopt.string = '未找到本地Manifest文件';
break;
case jsb.EventAssetsManager.UPDATE_PROGRESSION:
let percent = event.getPercent().toFixed(2);
this.percentLabel.string = parseInt(percent * 100) + '%';
break;
case jsb.EventAssetsManager.ERROR_DOWNLOAD_MANIFEST:
case jsb.EventAssetsManager.ERROR_PARSE_MANIFEST:
this.prmopt.string = '下载Manifest文件失败';
break;
case jsb.EventAssetsManager.ALREADY_UP_TO_DATE:
this.prmopt.string =