COCOS2D-JS Online Update Code script Dynamic update script hot update

Source: Internet
Author: User
Tags addchild

First, the basic idea of COCOS2D-JS dynamic update

  • The official beta release provides the Assetsmanager class to complete the dynamic update steps, Description: Https://github.com/chukong/cocos-docs/blob/master/manual/framework/html5/v3/assets-manager/zh.md
  • COCOS2D program, take Android as an example, the program exists in 2 places: APK installation directory (/data/ Dalvik-cache), APK Data directory (/data/data/[package name])
  • project.json file will be loaded before the program Main.js is started. Main.js does not need to be written to this list. So JS that needs to be updated dynamically cannot be listed in this JSON .

Second, the procedure release procedure

This article references: HTTPS://GITHUB.COM/FAINT2DEATH/COCOS2D-JS/BLOB/MASTER/ASSETSMANAGER.MD, but the configuration is different, this article is more biased to use the official configuration. According to the wording of the reference article, after many updates, the Project.manifest file will be very large, which affects the speed of user updates.

1, modify main.js, load Assetsmanager function

Cc.game.onStart =function() {cc.view.setDesignResolutionSize (800, 450, CC.     Resolutionpolicy.show_all); Cc.view.resizeWithBrowserSize (true); varFailcount = 0; varMaxfailcount = 1;//Maximum number of error retries    /** * Automatically update JS and resources*/     varAssetsmanagerloaderscene =cc. Scene.extend ({_am:NULL, _progress:NULL, _percent:0, run:function(){             if(!cc.sys.isNative) { This. Loadgame (); return; }            varLayer =Newcc.             Layer ();  This. AddChild (layer);  This. _progress =NewCc. Labelttf.create ("Update 0%", "Arial", 12);  This. _progress.x = CC.WINSIZE.WIDTH/2;  This. _progress.y = CC.WINSIZE.HEIGHT/2 + 50; Layer.addchild ( This. _progress); varStoragepath = (jsb.fileutils? Jsb.fileUtils.getWritablePath (): "./");  This. _am =NewJsb. Assetsmanager ("Res/project.manifest", Storagepath);  This. _am.retain (); if(! This. _am.getlocalmanifest (). isLoaded ()) {Cc.log ("Fail to update assets, step skipped.");  This. Loadgame (); }             Else             {                 varthat = This; varListener =NewCc. Eventlistenerassetsmanager ( This. _am,function(event) {Switch(Event.geteventcode ()) { Casecc. EventAssetsManager.ERROR_NO_LOCAL_MANIFEST:cc.log ("No local manifest file found, skip assets update.");                             That.loadgame ();  Break;  Casecc. Eventassetsmanager.update_progression:that._percent=event.getpercent (); Cc.log (That._percent+ "%"); varmsg =Event.getmessage (); if(msg) {Cc.log (msg); }                              Break;  Casecc. Eventassetsmanager.error_download_manifest: Casecc. EventAssetsManager.ERROR_PARSE_MANIFEST:cc.log ("Fail to download manifest file, update skipped.");                             That.loadgame ();  Break;  Casecc. EventAssetsManager.ALREADY_UP_TO_DATE:cc.log ("Already_up_to_date.");                             That.loadgame ();  Break;  Casecc. EventAssetsManager.UPDATE_FINISHED:cc.log ("Update finished.");                             That.loadgame ();  Break;  Casecc. EventAssetsManager.UPDATE_FAILED:cc.log ("Update failed." +event.getmessage ()); Failcount++; if(Failcount <Maxfailcount)                             {that._am.downloadfailedassets (); }                             Else{Cc.log ("Reach maximum fail count, exit update process"); Failcount= 0;                             That.loadgame (); }                              Break;  Casecc. EventAssetsManager.ERROR_UPDATING:cc.log ("Asset Update Error:" + event.getassetid () + "," +event.getmessage ());                             That.loadgame ();  Break;  Casecc.                             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(){             //Jslist is the variable of jslist.js, record all JS. Cc.loader.loadJs (["Src/jslist.js"],function() {cc.loader.loadJs (jslist,function() {Cc.director.runScene (NewMainscene ());             });         }); }, UpdateProgress:function(DT) { This. _progress.string = "Update" + This. _percent + "%"; }, OnExit:function() {Cc.log ("Assetsmanager::onexit");  This. _am.release ();  This. _super ();         }     }); varScene =NewAssetsmanagerloaderscene (); Scene.run (); }; Cc.game.run ();

2, establish jslist.js. Using the fixed name jslist, this corresponds to the 1th step of the code.

var jslist = [     "Src/resource.js",     "Src/app.js" ]

3, modify the Project.json. Add the extensions module to delete the contents of the jslist.

{     "Project_type": "JavaScript",    "DebugMode": 1,     true,     "framerate":     "id": "Gamecanvas"     ,"Rendermode": 0,     " Enginedir ":" FRAMEWORKS/COCOS2D-HTML5 ",    " modules ": [" cocos2d "," extensions "],    " Jslist " : [              ]}

4, the project Res directory to add a project.manifest file, Assetsmanager.js will use. The URL fills in the address of the server, PackageURL is the storage directory for the files to be updated dynamically.

{     "PackageURL": "Http://192.168.1.11:8000/res",     "Remotemanifesturl": "/HTTP// 192.168.1.11:8000/res/project.manifest ",     " Remoteversionurl ":" http://192.168.1.11:8000/res/ Version.manifest ",     " version ":" 1.0.1 ",     " engineversion ":" 3.0 rc0 ",     " Assets " : {    },    " Searchpaths " : [     ]}

5, packaging procedures. You can already run the underlying version even if you don't have a network at this point.

Third, dynamic update test

1, the server placed Version.manifest and the new project.manifest.

Assetsmanager will check version.manifest first to determine if there is an update. If there is, then pull the project.manifest. It can be said that Version.manifest is a smaller version of the Project.manifest, only the first few lines, the two agree.

Version.manifest:

{     "PackageURL": "Http://192.168.1.11:8000/res",     "Remotemanifesturl": "/HTTP// 192.168.1.11:8000/res/project.manifest ",     " Remoteversionurl ":" http://192.168.1.11:8000/res/ Version.manifest ",     " version ":" 1.0.1 ",     " engineversion ":" 3.0 rc0 " }

Project.manifest:

{     "PackageURL": "Http://192.168.1.11:8000/res",     "Remotemanifesturl": "/HTTP// 192.168.1.11:8000/res/project.manifest ",     " Remoteversionurl ":" http://192.168.1.11:8000/res/ Version.manifest ",     " version ":" 1.0.1 ",     " engineversion ":" 3.0 rc0 ",     " Assets " : {         " Src/app.zip ": {             " MD5 ":" d07d260d8072f786a586a6a430d0e98b ",              true         }     },    "Searchpaths" : [     ]}

Manifest here used the official note not mentioned in the Compressed,src/app.zip is not in the initial packaging program, this is only updated. Specifies that the Compressed=true,assetsmanager download will automatically unzip the file and keep the file. This reduces the file size of the network transfer.

App.zip compression is app.js, after decompression will overwrite the initialization of the installation of app.js, thus enabling dynamic update.

Here can be updated several times, constantly update the version number, each time Assetsmanager will check whether the file exists, the file MD5 is consistent, if not present or MD5 inconsistency will be downloaded again.

2, no need to repack the release, directly open the COCOS2D program, you can see the word update, if you open the Logcat, you can also see the corresponding log.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.