Asynchronously dynamic loading of images or Plist files in a Cocos2d-x

Source: Internet
Author: User
This chapter only shares the logic of asynchronous loading. If there is anything wrong, I hope you can point it out in time. Currently, there are four types of resources that need to be asynchronously loaded in my code: 1.png. a single png is rarely used. 2. plist and png package resources together 3. plist and pvr. ccz package resources together 4. exported by CocosStudio. exportJson suffix animation file again

This chapter only shares the logic of asynchronous loading. If there is anything wrong, I hope you can point it out in time. Currently, there are four types of resources that need to be asynchronously loaded in my code: 1.png. a single png is rarely used. 2. plist and png package resources together 3. plist and pvr. ccz package resources together 4. exported by CocosStudio. exportJson suffix animation file again

This chapter only shares the logic of asynchronous loading. If there is anything wrong, I hope you can point it out in time.


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

1. png. A single png is rarely used.

2. Packaging resources of plist and png

3. Packaging resources of plist and pvr. ccz

4. animation files with the. ExportJson suffix exported by CocosStudio


Repeat the loading mechanism: all resources are loaded in order, and multiple files cannot be loaded at the same time. The processing of asynchronous loading must be written in void update (float f) to load data by frame.


Asynchronous loading of 1.png

1

2

Director::getInstance()->getTextureCache()->addImageAsync(reloadImages[curReloadImgNum],

CC_CALLBACK_1(LoadingScene::imageAsyncCallback, this));


2. Packaging resources of plist and png

There is not much nonsense. The method is directly used, and the plist api is not loaded. We use the method such as 1 to load png and then load plist In the callback function.

1

2

Director::getInstance()->getTextureCache()->addImageAsync(reloadPlists[curReloadPlistNum]+"png",

CC_CALLBACK_1(LoadingScene::plistImageAsyncCallback, this));

Pay attention to the callback function parameters. This is the key to plist asynchronous loading, and the shape parameter is the png texture.

1

2

3

4

5

6

void LoadingScene::plistImageAsyncCallback(cocos2d::Texture2D* texture)

{

SpriteFrameCache::getInstance()->addSpriteFramesWithFile(reloadPlists[curReloadPlistNum].append("plist"), texture);

curReloadPlistNum++;

loading = true;

}

3. Packaging resources of plist and pvr. ccz

Similar to method 2.

1

2

3

4

5

6

7

8

Director::getInstance()->getTextureCache()->addImageAsync(reloadPvrPlists[curReloadPvrPlistNum] + "pvr.ccz",

CC_CALLBACK_1(LoadingScene::pvrPlistImageAsyncCallback, this));

void LoadingScene::pvrPlistImageAsyncCallback(cocos2d::Texture2D* texture)

{

SpriteFrameCache::getInstance()->addSpriteFramesWithFile(reloadPvrPlists[curReloadPvrPlistNum].append("plist"), texture);

curReloadPvrPlistNum++;

loading = true;

}

4. animation files with the. ExportJson suffix exported by Cocos Studio

1

2

3

4

5

6

7

ArmatureDataManager::getInstance()->addArmatureFileInfoAsync(reloadExportJsons[curReloadExportJsonNum],

this, schedule_selector(LoadingScene::jsonAsyncCallback));

void LoadingScene::jsonAsyncCallback(float f)

{

curReloadExportJsonNum++;

loading = true;

}

The four loading methods have been introduced. You only need to load them in order in update.


How to optimize memory and load as few resources as possible?

This scenario is called LoadingScene. This is a transit scenario. It is used to switch between two scenarios and release the resources of the previous scenario and pre-load the resources of the next scenario.


In this scenario, we can release all the files in the cache and load the required files according to the content in the next scenario. This requires us to do a good job of configuration, such as defending the radish. There are only two types of towers at the next level. We only need to load the animation of the two types of towers, instead of loading all the towers.


There is also a tip about asynchronously loading exportJson files. This asynchronous loading can load both the animation cache and the plist and png with it to the cache. If you want to load plist, you can also convert them into static animations and load them using exportJson to use the genie in them.

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.