Here is said in the need to use loading pictures, such as the game of a certain level of the picture, do not have to load in the beginning of the game (in case the user can not play that off, it is very unjust, traffic costs so much), otherwise loading speed is slow. This way of loading resources to be used in the Cc.loader Official document is introduced (HTTP://WWW.COCOS2D-X.ORG/DOCS/MANUAL/FRAMEWORK/HTML5/V3/CC-LOADER/ZH), mainly has
- Loadjs
- Loadjswithimg
- Loadtxt
- Loadbinary
- Loadimg
- Loadjson
The documentation gives an example of the following:
function (err, data) { ifreturn console.log ("Load Failed"); // success});
The second of the visible loadtxt is a callback function (called after the AJAX data is returned), which is not said in the document for starting the Load series function parameters, in fact, the first parameter of the callback function indicates whether there was an error (success is null), The second parameter, data, represents the information returned by the request (different load series functions parse the returned data, which is the result of the parse, such as loadimg). In fact, the first time you look at the document did not see the sample code, all the callback function has only one parameter of the sample code, then I think ah, how to fetch this asynchronous call data Ah? The document does not say a sound, had to look at the source, the benefits of open source is this, the following is the code of Loadbinary:
/** * Load binary data by URL. * @function * @param {String} URL * @param {function} [CB]*/cc.loader.loadBinary=function(URL, cb) {varSelf = This; varXHR = This. Getxmlhttprequest (), Errinfo= "Load" + URL + "failed!"; Xhr.open ("GET", URL,true); if(/msie/i.test (navigator.useragent) &&!/opera/i.test (navigator.useragent)) { //ie-specific Logic hereXhr.setrequestheader ("Accept-charset", "x-user-defined"); Xhr.onreadystatechange=function () { if(Xhr.readystate = = 4 && xhr.status = 200) { varFileContents = Cc._convertresponsebodytotext (xhr["Responsebody"]); CB (NULL, Self._str2uint8array (filecontents)); } ElseCB (ERRINFO); }; } Else { if(Xhr.overridemimetype) Xhr.overridemimetype ("Text\/plain; Charset=x-user-defined "); Xhr.onload=function() {xhr.readystate= = 4 && xhr.status = = 200? cbNULL, Self._str2uint8array (Xhr.responsetext)): CB (ERRINFO); }; } xhr.send (NULL);}; Cc.loader._str2uint8array=function(strdata) {if(!strdata)return NULL; varArrdata =NewUint8array (strdata.length); for(vari = 0; i < strdata.length; i++) {Arrdata[i]= Strdata.charcodeat (i) & 0xFF; } returnarrdata;};
You can see the call to the callback function here:
Xhr.readystate = = 4 && xhr.status = 200? CB (NULL, Self._str2uint8array (Xhr.responsetext)): CB (ERRINFO);
Then give me an example of your own use (loading the parameters and pictures of the game level):
/*helper function to fetch level data using AJAX*/varFetch_level =function(level, callback, Callback_data) {Cc.loader.loadJson ("api/level/" + level,function(x, New_level_data) {Cc.log ("Fetch level JSON data:", New_level_data); Current_level_data=New_level_data; CC.LOADER.LOADIMG (Current_level_data.photo,function(x, img) {cc.log ("Fetch level photo img:", IMG); Current_level_data.img=img; if(!!callback) {callback (Callback_data); } }) })};
COCOS2D-JS Development Record: Asynchronous loading of image data resources, etc.