• cocos2d-js 热更新模块 使用AssetsManager


    原帖子地址:http://cn.cocos2d-x.org/tutorial/show?id=1186

    在这个文章中原作者已经说的很清楚,我在这个其他改动一些适用我项目中需求

    1.满足Web和Native一套工程:为了维护方便

    2.检测更新每天只执行一次:每次打开会等待,一天打开多次会卡多次。这个改动只适合自己的

    AssetsManager 类:

    [javascript] view plain copy
     
    1. var __failCount = 0;  
    2.   
    3.   
    4. toFix = function(num, length) {  
    5.     return ('' + num).length < length ? ((new Array(length + 1)).join('0') + num).slice(-length) : '' + num;  
    6. };  
    7.   
    8.   
    9. var AssetsManagerLoaderScene = cc.Scene.extend({  
    10.     _am:null,  
    11.     _progress:null,  
    12.     _percent:0,  
    13.     _percentByFile:0,  
    14.     _progressText : "",  
    15.     run:function(){  
    16.         if (!cc.sys.isNative){  
    17.             this.loadGame();  
    18.             return;  
    19.         }  
    20.         else{  
    21.             var myDate = new Date();  
    22.             var year = myDate.getFullYear();  
    23.             var month = toFix(myDate.getMonth() + 1,2);  
    24.             var day = toFix(myDate.getDate(),2);  
    25.             var nowDate = year + "" + month + "" + day;  
    26.             var lastDate = cc.sys.localStorage.getItem("kAssetsDate");  
    27.   
    28.   
    29.             //一天只检测一次  
    30.             if(parseInt(nowDate) > parseInt(lastDate)  
    31.                 || lastDate == null  
    32.                 || lastDate == undefined  
    33.                 || lastDate == ""){  
    34.                 cc.sys.localStorage.setItem("kAssetsDate",nowDate);  
    35.             }  
    36.             else{  
    37.                 this.loadGame();  
    38.                 return;  
    39.             }  
    40.         }  
    41.   
    42.   
    43.         var layer = new cc.Layer();  
    44.         this.addChild(layer);  
    45.         this._progressText = "升级中 ";  
    46.         if(cc.sys.language == cc.sys.LANGUAGE_ENGLISH){  
    47.             this._progressText = "UPDATING ";  
    48.         }  
    49.   
    50.   
    51.         this._progress = new cc.LabelTTF(this._progressText +"0.00%", "Arial", 40);  
    52.         this._progress.x = cc.winSize.width / 2;  
    53.         this._progress.y = cc.winSize.height / 2 + 50;  
    54.         layer.addChild(this._progress);  
    55.   
    56.   
    57.         // android: /data/data/com.huanle.magic/files/  
    58.         var storagePath = (jsb.fileUtils ? jsb.fileUtils.getWritablePath() : "/");  
    59.         cc.log("AssetsManagerLoaderScene storagePath="+storagePath);  
    60.         this._am = new jsb.AssetsManager("res/project.manifest", storagePath);  
    61.         this._am.retain();  
    62.   
    63.   
    64.         if (!this._am.getLocalManifest().isLoaded())  
    65.         {  
    66.             cc.log("Fail to update assets, step skipped.");  
    67.             this.loadGame();  
    68.         }  
    69.         else  
    70.         {  
    71. //            cc.log("packageUrl = "+);  
    72.   
    73.   
    74.             var that = this;  
    75.             var listener = new jsb.EventListenerAssetsManager(this._am, function(event) {  
    76.                 cc.log("listener................");  
    77.                 switch (event.getEventCode()){  
    78.                     case jsb.EventAssetsManager.ERROR_NO_LOCAL_MANIFEST:  
    79.                         cc.log("No local manifest file found, skip assets update.");  
    80.                         that.loadGame();  
    81.                         break;  
    82.                     case jsb.EventAssetsManager.UPDATE_PROGRESSION:  
    83.                         that._percent = event.getPercent();  
    84.                         that._percentByFile = event.getPercentByFile();  
    85.                         cc.log(that._percent + "%");  
    86.   
    87.   
    88.                         var msg = event.getMessage();  
    89.                         if (msg) {  
    90.                             cc.log(msg);  
    91.                         }  
    92.                         break;  
    93.                     case jsb.EventAssetsManager.ERROR_DOWNLOAD_MANIFEST:  
    94.                     case jsb.EventAssetsManager.ERROR_PARSE_MANIFEST:  
    95.                         cc.log("Fail to download manifest file, update skipped.");  
    96.                         that.loadGame();  
    97.                         break;  
    98.                     case jsb.EventAssetsManager.ALREADY_UP_TO_DATE:  
    99.                     case jsb.EventAssetsManager.UPDATE_FINISHED:  
    100.                         cc.log("Update finished.");  
    101.                         that.loadGame();  
    102.                         break;  
    103.                     case jsb.EventAssetsManager.UPDATE_FAILED:  
    104.                         cc.log("Update failed. " + event.getMessage());  
    105.   
    106.   
    107.                         __failCount ++;  
    108.                         if (__failCount < 5)  
    109.                         {  
    110.                             that._am.downloadFailedAssets();  
    111.                         }  
    112.                         else  
    113.                         {  
    114.                             cc.log("Reach maximum fail count, exit update process");  
    115.                             __failCount = 0;  
    116.                             that.loadGame();  
    117.                         }  
    118.                         break;  
    119.                     case jsb.EventAssetsManager.ERROR_UPDATING:  
    120.                         cc.log("Asset update error: " + event.getAssetId() + ", " + event.getMessage());  
    121. //                        that.loadGame();  
    122.                         break;  
    123.                     case jsb.EventAssetsManager.ERROR_DECOMPRESS:  
    124.                         cc.log(event.getMessage());  
    125.                         that.loadGame();  
    126.                         break;  
    127.                     default:  
    128.                         break;  
    129.                 }  
    130.             });  
    131.   
    132.   
    133.             cc.eventManager.addListener(listener, 1);  
    134.             this._am.update();  
    135.             cc.director.runScene(this);  
    136.         }  
    137.   
    138.   
    139.         this.schedule(this.updateProgress, 0.5);  
    140.     },  
    141.     loadGame:function(){  
    142.         cc.log("loadGame");  
    143.         if (cc.sys.isNative){  
    144.             cc.loader.loadJs(["src/files.js"], function(err){  
    145.                 cc.loader.loadJs(jsFiles, function(err){  
    146.                     cc.LoaderScene.preload(g_resources, function () {  
    147.                         cc.director.runScene(new MainScene());  
    148.                     }, this);  
    149.                 });  
    150.             });  
    151.         }  
    152.         else{  
    153.             cc.LoaderScene.preload(g_resources, function () {  
    154.                 cc.director.runScene(new MainScene());  
    155.             }, this);  
    156.         }  
    157.     },  
    158.     updateProgress:function(dt){  
    159.         this._progress.string = this._progressText + this._percent.toFixed(2)+"%";//"" + this._percent;  
    160.     },  
    161.     onExit:function(){  
    162.         cc.log("AssetsManager::onExit");  
    163.   
    164.   
    165.         this._am.release();  
    166.         this._super();  
    167.     }  
    168. });  
  • 相关阅读:
    滑动拼图
    CentOS8安装中文输入法
    windows+ubuntu 双系统时间不一致的问题
    Goland 2019下载和安装(带破解补丁和汉化包)
    防火墙站名资源脚本
    linux上以服务方式启动程序kestrel
    NLog实践记录
    sqlserver安装ubuntu
    pyspark提交集群任务
    无法打开hadoop的UI页面
  • 原文地址:https://www.cnblogs.com/kefeiGame/p/7656960.html
Copyright © 2020-2023  润新知