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

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

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

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

AssetsManager 类:

  1. var __failCount = 0;
  2. toFix = function(num, length) {
  3. return ('' + num).length < length ? ((new Array(length + 1)).join('0') + num).slice(-length) : '' + num;
  4. };
  5. var AssetsManagerLoaderScene = cc.Scene.extend({
  6. _am:null,
  7. _progress:null,
  8. _percent:0,
  9. _percentByFile:0,
  10. _progressText : "",
  11. run:function(){
  12. if (!cc.sys.isNative){
  13. this.loadGame();
  14. return;
  15. }
  16. else{
  17. var myDate = new Date();
  18. var year = myDate.getFullYear();
  19. var month = toFix(myDate.getMonth() + 1,2);
  20. var day = toFix(myDate.getDate(),2);
  21. var nowDate = year + "" + month + "" + day;
  22. var lastDate = cc.sys.localStorage.getItem("kAssetsDate");
  23. //一天只检测一次
  24. if(parseInt(nowDate) > parseInt(lastDate)
  25. || lastDate == null
  26. || lastDate == undefined
  27. || lastDate == ""){
  28. cc.sys.localStorage.setItem("kAssetsDate",nowDate);
  29. }
  30. else{
  31. this.loadGame();
  32. return;
  33. }
  34. }
  35. var layer = new cc.Layer();
  36. this.addChild(layer);
  37. this._progressText = "升级中 ";
  38. if(cc.sys.language == cc.sys.LANGUAGE_ENGLISH){
  39. this._progressText = "UPDATING ";
  40. }
  41. this._progress = new cc.LabelTTF(this._progressText +"0.00%", "Arial", 40);
  42. this._progress.x = cc.winSize.width / 2;
  43. this._progress.y = cc.winSize.height / 2 + 50;
  44. layer.addChild(this._progress);
  45. // android: /data/data/com.huanle.magic/files/
  46. var storagePath = (jsb.fileUtils ? jsb.fileUtils.getWritablePath() : "/");
  47. cc.log("AssetsManagerLoaderScene storagePath="+storagePath);
  48. this._am = new jsb.AssetsManager("res/project.manifest", storagePath);
  49. this._am.retain();
  50. if (!this._am.getLocalManifest().isLoaded())
  51. {
  52. cc.log("Fail to update assets, step skipped.");
  53. this.loadGame();
  54. }
  55. else
  56. {
  57. //            cc.log("packageUrl = "+);
  58. var that = this;
  59. var listener = new jsb.EventListenerAssetsManager(this._am, function(event) {
  60. cc.log("listener................");
  61. switch (event.getEventCode()){
  62. case jsb.EventAssetsManager.ERROR_NO_LOCAL_MANIFEST:
  63. cc.log("No local manifest file found, skip assets update.");
  64. that.loadGame();
  65. break;
  66. case jsb.EventAssetsManager.UPDATE_PROGRESSION:
  67. that._percent = event.getPercent();
  68. that._percentByFile = event.getPercentByFile();
  69. cc.log(that._percent + "%");
  70. var msg = event.getMessage();
  71. if (msg) {
  72. cc.log(msg);
  73. }
  74. break;
  75. case jsb.EventAssetsManager.ERROR_DOWNLOAD_MANIFEST:
  76. case jsb.EventAssetsManager.ERROR_PARSE_MANIFEST:
  77. cc.log("Fail to download manifest file, update skipped.");
  78. that.loadGame();
  79. break;
  80. case jsb.EventAssetsManager.ALREADY_UP_TO_DATE:
  81. case jsb.EventAssetsManager.UPDATE_FINISHED:
  82. cc.log("Update finished.");
  83. that.loadGame();
  84. break;
  85. case jsb.EventAssetsManager.UPDATE_FAILED:
  86. cc.log("Update failed. " + event.getMessage());
  87. __failCount ++;
  88. if (__failCount < 5)
  89. {
  90. that._am.downloadFailedAssets();
  91. }
  92. else
  93. {
  94. cc.log("Reach maximum fail count, exit update process");
  95. __failCount = 0;
  96. that.loadGame();
  97. }
  98. break;
  99. case jsb.EventAssetsManager.ERROR_UPDATING:
  100. cc.log("Asset update error: " + event.getAssetId() + ", " + event.getMessage());
  101. //                        that.loadGame();
  102. break;
  103. case jsb.EventAssetsManager.ERROR_DECOMPRESS:
  104. cc.log(event.getMessage());
  105. that.loadGame();
  106. break;
  107. default:
  108. break;
  109. }
  110. });
  111. cc.eventManager.addListener(listener, 1);
  112. this._am.update();
  113. cc.director.runScene(this);
  114. }
  115. this.schedule(this.updateProgress, 0.5);
  116. },
  117. loadGame:function(){
  118. cc.log("loadGame");
  119. if (cc.sys.isNative){
  120. cc.loader.loadJs(["src/files.js"], function(err){
  121. cc.loader.loadJs(jsFiles, function(err){
  122. cc.LoaderScene.preload(g_resources, function () {
  123. cc.director.runScene(new MainScene());
  124. }, this);
  125. });
  126. });
  127. }
  128. else{
  129. cc.LoaderScene.preload(g_resources, function () {
  130. cc.director.runScene(new MainScene());
  131. }, this);
  132. }
  133. },
  134. updateProgress:function(dt){
  135. this._progress.string = this._progressText + this._percent.toFixed(2)+"%";//"" + this._percent;
  136. },
  137. onExit:function(){
  138. cc.log("AssetsManager::onExit");
  139. this._am.release();
  140. this._super();
  141. }
  142. });
05-23 13:37