1.launch.js代码:
// launch: update files
var __failCount = 0;
var AssetsManager = cc.Scene.extend({
_am: null,
_progress: null,
_percent: 0,
_percentByFile: 0,
run: function () {
// windows may not need update
var not_update;
if (cc.sys.os == sys.OS_WINDOWS) {
not_update = 1;
}
if (not_update || !cc.sys.isNative) {
this.loadGame();
return;
}
var layer = new cc.Layer();
this.addChild(layer);
this._progress = new cc.LabelTTF.create("下载进度:0%", "Arial", 12);
this._progress.x = cc.winSize.width / 2;
this._progress.y = cc.winSize.height / 2 + 50;
layer.addChild(this._progress);
// android: /data/data/com.huanle.magic/files/
var storagePath = (jsb.fileUtils ? jsb.fileUtils.getWritablePath() : "./");// storagePath表示本地的存储目录,更新的文件将下载到此目录下,程序加载脚本和资源时,也将优先从此目录查询
this._am = new jsb.AssetsManager("res/project.manifest", storagePath);
this._am.retain();
if (!this._am.getLocalManifest().isLoaded()) {
cc.log("Fail to update assets, step skipped.");
this.loadGame();
} else {
var that = this;
var listener = new jsb.EventListenerAssetsManager(this._am, function (event) {
switch (event.getEventCode()) {
case jsb.EventAssetsManager.ERROR_NO_LOCAL_MANIFEST:
cc.log("No local manifest file found, skip assets update.");
that.loadGame();
break;
case jsb.EventAssetsManager.UPDATE_PROGRESSION:
that._percent = event.getPercent();
that._percentByFile = event.getPercentByFile();
cc.log(that._percent + "%");
var msg = event.getMessage();
if (msg) {
cc.log(msg);
}
break;
case jsb.EventAssetsManager.ERROR_DOWNLOAD_MANIFEST:
case jsb.EventAssetsManager.ERROR_PARSE_MANIFEST:
cc.log("Fail to download manifest file, update skipped.");
that.loadGame();
break;
case jsb.EventAssetsManager.ALREADY_UP_TO_DATE:
case jsb.EventAssetsManager.UPDATE_FINISHED:
cc.log("Update finished.");
that.loadGame();
break;
case jsb.EventAssetsManager.UPDATE_FAILED:
cc.log("Update failed. " + event.getMessage());
__failCount++;
if (__failCount < 5) {
that._am.downloadFailedAssets();
} else {
cc.log("Reach maximum fail count, exit update process");
__failCount = 0;
that.loadGame();
}
break;
case jsb.EventAssetsManager.ERROR_UPDATING:
cc.log("Asset update error: " + event.getAssetId() + ", " + event.getMessage());
that.loadGame();
break;
case jsb.EventAssetsManager.ERROR_DECOMPRESS:
cc.log(event.getMessage());
that.loadGame();
break;
default:
break;
}
});
cc.eventManager.addListener(listener, 1);
this._am.update();
cc.director.runScene(this);
}
this.schedule(this.updateProgress, 0.5);
},
loadGame: function () {
cc.loader.loadJs(["src/game.js"], function (err) {
if (err) {
cc.log("error:" + err);
} else {
// run game logic
game.run();
}
});
},
updateProgress: function (dt) {
this._progress.string = "下载进度:" + this._percent + "%";
},
onExit: function () {
this._am.release();
this._super();
}
});
2.配置文件
在客户端的res目录下添加project.manifest:
{
"packageUrl" : "http://192.168.1.103/bw",
"remoteManifestUrl" : "http://192.168.1.103/bw/project.manifest",
"remoteVersionUrl" : "http://192.168.1.103/bw/version.manifest",
"version" : "1.0.0",
"groupVersions" : {
"1" : "1.0.0"
}
}
project.manifest:
{
"packageUrl" : "http://192.168.1.103/bw",
"remoteManifestUrl" : "http://192.168.1.103/bw/project.manifest",
"remoteVersionUrl" : "http://192.168.1.103/bw/version.manifest",
"version" : "1.0.5",
"groupVersions" : {
"1" : "1.0.5"
},
"engineVersion" : "3.0 rc0",
"assets" : {
"src/app.zip" : {
"md5" : "6809C100A369FD8A79A97208B0278A2B",
"compressed" : true,
"group" : "1"
}
},
"searchPaths" : [
"src/"
]
}
{
"packageUrl" : "http://192.168.1.103/bw",
"remoteManifestUrl" : "http://192.168.1.103/bw/project.manifest",
"remoteVersionUrl" : "http://192.168.1.103/bw/version.manifest",
"version" : "1.0.5",
"groupVersions": {
"1" : "1.0.5"
},
"engineVersion" : "3.0 rc0"
}