cocos2dx3.2 asynchronous loading and dynamic loading

Source: Internet
Author: User

Half a month did not update the blog, from the project began to learn a lot of details of things, are not very system, but it is the development of the online must experience things. For example, the right asynchronous loading in the Super Marie series (i), proper layering, proper compositing and loading of plist, timely removal of unused caches, the resources required to dynamically load the current level according to the level.

This chapter only shares the logic of asynchronous loading, if there is a wrong place also hope that you point out in time.

Currently, there are four types of resources in my code that need to be loaded asynchronously:

1.png single PNG in less case

Packaged resources with 2.plist and PNG

Packaging resources for 3.plist and PVR.CCZ

4.CocosStudio exported. exportjson suffix Animation file


Repeat the loading mechanism: all resources are loaded sequentially and cannot load multiple files at the same time. The process of performing an asynchronous load is definitely written in void update (float f) and is loaded by frame.

asynchronous loading of 1.png

[CPP] view plain copy print? Director::getinstance ()->gettexturecache ()->addimageasync (Reloadimages[curreloadimgnum], CC_CALLBA Ck_1 (Loadingscene::imageasynccallback, this));

packaged resources with 2.plist and PNG

Not much nonsense, directly on the method, without loading the Plist API, we use a method such as 1 to load PNG, and then load the plist in the callback function.

[CPP] view plain copy print? Director::getinstance ()->gettexturecache ()->addimageasync (reloadplists[curreloadplistnum]+ "PNG", C C_callback_1 (loadingscene::p listimageasynccallback, this));

Note the parameters of the callback function, which is the key to plist asynchronous loading, and the formal parameter is a PNG texture.

[CPP] view plain copy print? void Loadingscene::p listimageasynccallback (cocos2d::texture2d* texture) {spriteframecache::getinstance ()->addS       Priteframeswithfile (Reloadplists[curreloadplistnum].append ("plist"), texture);       curreloadplistnum++;   Loading = true; }

Packaging resources for 3.plist and PVR.CCZ

A method similar to 2.

[CPP] view plain copy print? Director::getinstance ()->gettexturecache ()->addimageasync (Reloadpvrplists[curreloadpvrplistnum] + "Pvr.ccz   ", Cc_callback_1 (loadingscene::p vrplistimageasynccallback, this)); void Loadingscene::p vrplistimageasynccallback (cocos2d::texture2d* texture) {spriteframecache::getinstance ()->a       Ddspriteframeswithfile (Reloadpvrplists[curreloadpvrplistnum].append ("plist"), texture);       curreloadpvrplistnum++;   Loading = true; }

4.CocosStudio exported. exportjson suffix Animation file

[CPP]   View plain  copy  print? Armaturedatamanager::getinstance ()->addarmaturefileinfoasync (Reloadexportjsons[curreloadexportjsonnum],                     This, schedule_selector (loadingscene::jsonasynccallback));   Void loadingscene:: Jsonasynccallback (float f)   

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.