CCProgress of cocos2dX UI control

Source: Internet
Author: User

Today, we are going to learn a very practical control provided by cocos2dX for us,CCProgressProgress bar. Let's take a look at some common functions.


Usage:

CCProgressTimer: Create (genie object );

SetType (progress bar style); // you can specify two optional parameters: kCCProgressTimerTypeBar and kCCProgressTimerTypeRadial)

SetPercentage (float); // set the current value between 0 and. Generally, the default value is 0.

GetPercentage (); // get the current value

SetReverseProgress (bool); // whether to enable reverse

SetBarChangeRate (CCPoint object); // defines the style at the beginning. Some people also say that it is set as an anchor. I usually set it to ccp (1, 0) and setMidpoint () set it to ccp (0, 1), which is a normal progress bar style.

SetMidpoint (CCPoint object); // timer display direction. The default value is 0.5, 0.5, starting from the middle.

Old Rules: Prepare resources:


First, create a progress bar and a label:

<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHByZSBjbGFzcz0 = "brush: java;"> // create the font tag CCLabelTTF * ttf = CCLabelTTF: create ("loading... % 0 "," Arial ", 24); ttf-> setPosition (ccp (visibleSize. width/2, visibleSize. height/2 + 140); addChild (ttf, 0,101); // create a progress bar background. You do not need CCSprite * parBg = CCSprite: create ("bg.png "); parBg-> setPosition (ccp (visibleSize. width/2, visibleSize. height/2); addChild (parBg); // create the progress bar CCProgressTimer * myTimer = ccprogresstmer: create (CCSprite: create ("t.png ")); myTimer-> setPosition (ccp (visibleSize. width/2, visibleSize. height/2); myTimer-> setType (kCCProgressTimerTypeBar); // set it to a bar progress bar myTimer-> setBarChangeRate (ccp (1, 0 )); myTimer-> setMidpoint (ccp (0, 1); myTimer-> setPercentage (0); // set the current value of addChild (myTimer, 0,102 );


Then we create an update function (you should remember it) for breaking the operation progress bar and labels, and then create a function for the operation after the progress is completed:

virtual void update(float delta);void loadOK();


Implementation functions:

Void HelloWorld: update (float delta) {ccprogresstmer * myTimer = (ccprogresstmer *) this-> getChildByTag (102); float cu = myTimer-> getPercentage (); // obtain the value of the current progress bar if (cu> = 100) {// if the progress bar is greater than 100, call the load completion function to stop the UPDATE function loadOK (); unscheduleUpdate ();} else {// otherwise, add 0.1cu + = 0.1f to the current progress; myTimer-> setPercentage (cu); CCString * str = CCString: createWithFormat ("loading... % 0.2f ", cu); CCLabelTTF * ttf = (CCLabelTTF *) this-> getChild ByTag (101); ttf-> setString (str-> getCString () ;}} void HelloWorld: loadOK () {CCLabelTTF * ttf = (CCLabelTTF *) this-> getChildByTag (101); ttf-> setString ("load OK !!!! ");}




Now, a loading interface is complete. Let's take a look at the effect:




Do you still remember the red police game? I believe many of my friends have been playing. when building a building, you can also use the progress bar. Let's take a look at how to use it.CCProgressTo:



CCProgressTo: create (Time consumed, show image percentage );


Let's look at the usage, and we will block the updated function:

CCProgressTo* to = CCProgressTo::create( 2.0f, 100.0f);CCCallFunc* func = CCCallFunc::create( this, callfunc_selector( HelloWorld::loadOK));myTimer->runAction( CCSequence::create( to, func, NULL));



Here we create an action sequence, and then execute a function task after the action is executed. CCCallFunc creates a function that is equivalent to an action, which we will discuss later, I didn't operate on that tag. Except for loading, it won't change. Don't care about these details (Narrator: You have never been so lazy)

Let's take a look at the effect:


No operation tag. It's not a bug.



Whether the effect has come out. Of course, the progress bar control can also be used for blood strip and slice. We can also use it for skill cooling, let's explore it by yourself ,,,




It is said that today's traffic jam has only arrived home, so I have to vomit about the traffic conditions in Chengdu.

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.