首页 > 代码库 > cocos3——4.js热更新
cocos3——4.js热更新
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(); } });
更新完后会加载src/game.js,然后game.run跑游戏的主逻辑,所有逻辑从那里开始就可以了。
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" } }
服务端可用everything搭建http做测试:
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/" ] }
version.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" }
做增量包的话要用groupVersions,可以参见http://cn.cocos2d-x.org/tutorial/show?id=1186
cocos3——4.js热更新
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。