cocos3——4.js熱更新

來源:互聯網
上載者:User

cocos3——4.js熱更新

1.launch.js代碼:

 

// launch: update filesvar __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,可以參見www.bkjia.com

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.