How to load picture resources in COCOS2DX, and how to get loaded picture resources from memory

Source: Internet
Author: User

Frequently used resources such as: sounds, pictures, plist files, loaded into memory in advance, to speed up the game's smoothness.

1. Pre-load Sound:

Simpleaudioengine::getinstance ()->preloadbackgroundmusic ("Boom.mp3");

It can be played directly after loading: Simpleaudioengine::getinstance ()->playbackgroundmusic ("Boom.mp3");

2. Pre-load the picture resource:

The loading of picture resources is divided into two ways: synchronous loading and asynchronous loading, and usually we use asynchronous loading.

Texturecache::getinstance ()->addimageasync ("Boss.png", Cc_callback_1 (Loadingscene::resourcecallback,this)); /resourcecallback is a function called after loading and can be used for statistical purposes.

Once loaded into memory, we can read directly from memory without the need for an IO operation:

There are two ways to read an already loaded picture resource from memory:

Auto BG = sprite::createwithtexture (Texturecache::getinstance ()->gettextureforkey ("groundlevel.jpg"));// The premise is that the previously loaded picture resource must be loaded, successful, because the asynchronous load just performs the load action, whether the load is successful, it is unknown.

The second method:

Auto BG = sprite::createwithtexture (Texturecache::getinstance ()->addimage ("groundlevel.jpg"));//Use the AddImage () method , most insured, if the picture is not preloaded, the IO operation is loaded into memory from the disk, and a texture2d* object is returned, and the memory is read directly if it has already been loaded before and exists in memory.

Comparison of the two methods:

The pursuit of Speed: The first method of reading, if you must ensure that the resources to read the picture has been loaded into memory

The pursuit of insurance: The second method, whether or not loaded, will get the correct results, but to ensure that the resource picture exists.

3. Load other resource files, such as plist, usually some 3D model files, or particle effects resource files

Fileutils::getinstance ()->getdatafromfile ("debris.plist");//Pre-load 3D model files

Fileutils::getinstance ()->getdatafromfile ("Vanishingpoint.plist"), or//pre-loading the plist file of the starry sky's particle effects

Spriteframecache::getinstance ()->addspriteframeswithfile ("menu_scene.plist");//pre-load packaged picture plist file

(The way to read a picture from an already loaded memory frame cache: Auto Sprite=sprite::createwithspriteframename ("Mainmenu_bg.png");//premise is mainmenu_ Bg.png is already loaded into the Framecache by plist or other methods)

How to load picture resources in COCOS2DX, and how to get loaded picture resources from memory

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.