Cocos2dx 3.0 improve article (10) Resource Loading progress bar Loading...

Source: Internet
Author: User

This blog post was supposed to be written last night, but this morning I am going to take a driver's license examination. What I fear most is that my eyesight cannot reach 5.0, after all, it takes too long for this phase to face the screen.
So I barely touched my computer last night, didn't play with my cell phone, and went to bed early and got up early. Health Check passed!


First, I want to talk about the progress bar. Well, wait. Put away the angry watermelon knife in your hand first. I also know that TestCpp also has this example. But TestCpp only has label changes, and I add a progress bar.
Please give me a nice name for this kind of method: Click it!


Well, the steps are as follows:
1. Create label and progressTimer;
2. Load resources. The callback function is called every time a resource is loaded;
3. After loading, enter the new interface.



First, check the header file HelloWorld. h.

# Ifndef _ HELLOWORLD_SCENE_H __# define _ HELLOWORLD_SCENE_H __# include "cocos2d. h "class HelloWorld: public cocos2d: Layer {public: HelloWorld (): m_numSp (20), m_loadedSp (0), loadProgress (NULL) {}; static cocos2d :: scene * createScene (); virtual bool init (); void loadingCallback (Object * pSender); // destroy the void gotoNewLayer () function after loading an image (); // after loading, the jump function CREATE_FUNC (HelloWorld); private: cocos2d: ProgressTimer * loadProgress; // progress bar cocos2d: LabelTTF * percentLabel; // loading progress labelcocos2d :: labelTTF * loadLabel; // display the loading: labelint m_numSp; // Number of genie to be loaded. The value is initialized to 20 int m_loadedSp; // Number of loaded genie }; # endif/_ HELLOWORLD_SCENE_H __

1. Create

Size visibleSize = Director: getInstance ()-> getVisibleSize (); Point origin = Director: getInstance ()-> getVisibleOrigin (); loadLabel = LabelTTF: create ("Loading: "," Arial ", 20); // create labelloadLabel-> setPosition (Point (visibleSize. width/2-30, visibleSize. height/2 + 30); this-> addChild (loadLabel, 1); percentLabel = LabelTTF: create ("0%", "Arial", 20 ); // create labelpercentLabel-> setPosition (Point (visibleSize. width/2 + 35, visibleSize. height/2 + 30); this-> addChild (percentLabel, 2); auto loadBg = Sprite: create ("sliderTrack.png "); // process bar basemap loadBg-> setPosition (Point (visibleSize. width/2, visibleSize. height/2); this-> addChild (loadBg, 1); loadProgress = ProgressTimer: create (Sprite: create ("sliderProgress.png ")); // create a process BAR loadProgress-> setBarChangeRate (Point (1, 0); // set the variation rate of the Process BAR loadProgress-> setType (ProgressTimer: Type: BAR ); // set the process Bar type loadProgress-> setMidpoint (Point (0, 1); // set the progress direction loadProgress-> setPosition (Point (visibleSize. width/2, visibleSize. height/2); loadProgress-> setPercentage (0.0f); // set the initial value to 0this-> addChild (loadProgress, 2 );

2. Attach Images

// Load 20 images. Call the callback function: loadingCallbackDirector: getInstance ()-> getTextureCache ()-> addImageAsync ("HelloWorld.png", this, callback (HelloWorld: loadingCallback); ctor: getInstance ()-> getTextureCache ()-> addImageAsync ("HelloWorld1.png", this, callfuncO_selector (HelloWorld: loadingCallback )); director: getInstance ()-> getTextureCache ()-> addImageAsync ("HelloWorld2.png", this, callfuncO_selector (HelloWorld: loadingCallback); Director: getInstance () -> getTextureCache ()-> addImageAsync ("HelloWorld3.png", this, callfuncO_selector (HelloWorld: loadingCallback); Director: getInstance ()-> getTextureCache () -> addImageAsync ("HelloWorld4.png", this, callback (HelloWorld: loadingCallback); Director: getInstance ()-> getTextureCache ()-> addImageAsync ("HelloWorld5.png", this, callback (HelloWorld: loadingCallback); ctor: getInstance ()-> getTextureCache ()-> addImageAsync ("HelloWorld6.png", this, callfuncO_selector (HelloWorld: loadingCallback )); director: getInstance ()-> getTextureCache ()-> addImageAsync ("HelloWorld7.png", this, callfuncO_selector (HelloWorld: loadingCallback); Director: getInstance () -> getTextureCache ()-> addImageAsync ("HelloWorld.png", this, callfuncO_selector (HelloWorld: loadingCallback); Director: getInstance ()-> getTextureCache () -> addImageAsync ("HelloWorld.png", this, convert (HelloWorld: loadingCallback); Director: getInstance ()-> getTextureCache ()-> addImageAsync ("HelloWorld.png", this, callback (HelloWorld: loadingCallback); ctor: getInstance ()-> getTextureCache ()-> addImageAsync ("HelloWorld.png", this, callfuncO_selector (HelloWorld: loadingCallback )); director: getInstance ()-> getTextureCache ()-> addImageAsync ("HelloWorld.png", this, callfuncO_selector (HelloWorld: loadingCallback); Director: getInstance () -> getTextureCache ()-> addImageAsync ("HelloWorld.png", this, callfuncO_selector (HelloWorld: loadingCallback); Director: getInstance ()-> getTextureCache () -> addImageAsync ("HelloWorld.png", this, convert (HelloWorld: loadingCallback); Director: getInstance ()-> getTextureCache ()-> addImageAsync ("HelloWorld.png", this, callback (HelloWorld: loadingCallback); ctor: getInstance ()-> getTextureCache ()-> addImageAsync ("HelloWorld.png", this, callfuncO_selector (HelloWorld: loadingCallback )); director: getInstance ()-> getTextureCache ()-> addImageAsync ("HelloWorld.png", this, callfuncO_selector (HelloWorld: loadingCallback); Director: getInstance () -> getTextureCache ()-> addImageAsync ("HelloWorld.png", this, callfuncO_selector (HelloWorld: loadingCallback); Director: getInstance ()-> getTextureCache () -> addImageAsync ("HelloWorld.png", this, callfuncO_selector (HelloWorld: loadingCallback ));

3. Callback Function after image loading:

Void HelloWorld: loadingCallback (Object * pSender) {++ m_loadedSp; // every time you enter this function, let m_loadedSp + 1 char buf_str [16]; sprintf (buf_str, "% d %", (int) (float) m_loadedSp/m_numSp) * 100), m_numSp); percentLabel-> setString (buf_str ); // update the value float newPercent of percentLabel = 100-(float) m_numSp-(float) m_loadedSp)/(float) m_numSp/100 ); // calculate the current percentage of the progress bar. // because the image loading speed is very fast, ProgressTo, // or ProgressFromTo is not used to update the progress bar loadProgress-> setPercentage (newPercent ); // update progress bar // if (m_loadedSp = m_numSp) {this-> removeChild (loadProgress) after the image is loaded ); // Delete the added objects from this-> removeChild (percentLabel); this-> removeChild (loadLabel); // after loading, you must jump to gotoNewLayer, here you can // create a new Scene, a new Layer, or any other messy this-> gotoNewLayer ();}}

4. Enter the new interface

Void HelloWorld: gotoNewLayer () {auto size = Director :: getInstance ()-> getWinSize (); auto sp = Sprite: create ("HelloWorld.png "); // use the image previously loaded into the cache to create an genie. Sp-> setPosition (Point (size. width/2, size. height/2); this-> addChild (sp, 1 );}


Because the comments in the Code are quite detailed, I will not talk too much nonsense.
Well, it's done. I wrote this article after work and it rained heavily outside. I have to rush back for dinner. Windy male paper.


Respect Original, reprint please indicate the original address: http://blog.csdn.net/start530? Viewmode = contents


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.