Progresstimer in Cocos2d-X

Source: Internet
Author: User
Zookeeper

Progresstimer is a progress bar, which is widely used in game development. For example, in some fighting games, it is essential to display changes in blood and game loading progress.

Use ccprogresstimer in the Cocos2d-X to create a progress bar

First, use progresstimer to shield some genie from a simple example.

First, put an image in the resource folder under the project folder.

Then create a progresstimer class

Add the following code to progresstimer. h:

#ifndef __ProgressTimer_H__#define __ProgressTimer_H__#include "cocos2d.h"USING_NS_CC;class ProgressTimer : public CCLayer{public:    virtual bool init();      static CCScene* scene();        CREATE_FUNC(ProgressTimer);   void scheduleFunc(float dt);};#endif // __ProgressTimer_H__

Add the following code to progresstimer. cpp:

# Include "progresstimer. H "ccscene * progresstimer: Scene () {ccscene * scene = ccscene: Create (); progresstimer * layer = progresstimer: Create (); scene-> addchild (layer); Return scene;} bool progresstimer: Init () {// initialize the parent class cclayer: Init (); // obtain the size of the Form. ccsize winsize = ccdirector: shareddire()-> getwinsize (); // set the coordinate ccpoint center = CCP (winsize. width/2, winsize. height/2); // create the genie ccsprite * sprite = ccsprite: Create ("labelatlasimg.png"); // use the genie to create the progress bar ccprogresstimer * progress = ccprogresstimer :: create (sprite); addchild (Progress); // set the progress bar position progress-> setposition (center); // set the percentage (display 50% of the progress bar) progress-> setpercentage (50); Return true ;}

Running result:


Instance 2: display the first half

# Include "progresstimer. H "ccscene * progresstimer: Scene () {ccscene * scene = ccscene: Create (); progresstimer * layer = progresstimer: Create (); scene-> addchild (layer); Return scene;} bool progresstimer: Init () {// initialize the parent class cclayer: Init (); // obtain the size of the Form. ccsize winsize = ccdirector: shareddire()-> getwinsize (); // set the coordinate ccpoint center = CCP (winsize. width/2, winsize. height/2); // create the genie ccsprite * sprite = ccsprite: Create ("labelatlasimg.png"); // use the genie to create the progress bar ccprogresstimer * progress = ccprogresstimer :: create (sprite); addchild (Progress); // set the progress bar position progress-> setposition (center ); // set the progress bar mode // kccprogresstimertypebar indicates the bar mode // the default mode is kccprogresstimertyperadial (circle mode) Progress-> settype (kccprogresstimertypebar ); // set the direction of the progress bar change // setmidpoint is on the left by default // CCP () indicates that the direction of the X axis has changed, but the direction of the Y axis has not changed // CCP) indicates that there is no change in the X axis direction, and there is a change in the Y axis direction progress-> setbarchangerate (CCP (); // from which the change starts // CCP) indicates that progress-> setmidpoint (CCP (50%) is changed from the start of the left; // sets the percentage (of the running progress bar is displayed) Progress-> setpercentage (50); Return true ;}


Running result:



Progress bar for sstimer to achieve the rotation effect

Program code:

# Include "progresstimer. H "ccscene * progresstimer: Scene () {ccscene * scene = ccscene: Create (); progresstimer * layer = progresstimer: Create (); scene-> addchild (layer); Return scene;} bool progresstimer: Init () {// initialize the parent class cclayer: Init (); // obtain the size of the Form. ccsize winsize = ccdirector: shareddire()-> getwinsize (); // set the coordinate ccpoint center = CCP (winsize. width/2, winsize. height/2); // create the genie ccsprite * sprite = ccsprite: Create ("labelatlasimg.png"); // use the genie to create the progress bar ccprogresstimer * progress = ccprogresstimer :: create (sprite); addchild (Progress); // set the progress bar position progress-> setposition (center); // set progress ID to 100 progress-> settag (100 ); // define a timer schedule (schedule_selector (progressttor: schedulefunc), 0.5f); Return true;} void progresstimer: schedulefunc (float DT) {// get the progress bar ccprogresstimer * progress = (ccprogresstimer *) getchildbytag (100); Progress-> setpercentage (Progress-> getpercentage () + 1) through the progress bar ID ); if (Progress-> getpercentage () >=100) {unscheduleallselectors ();}}

Running result:


Progress bar 2

Program code:

# Include "progresstimer. H "ccscene * progresstimer: Scene () {ccscene * scene = ccscene: Create (); progresstimer * layer = progresstimer: Create (); scene-> addchild (layer); Return scene;} bool progresstimer: Init () {// initialize the parent class cclayer: Init (); // obtain the size of the Form. ccsize winsize = ccdirector: shareddire()-> getwinsize (); // set the coordinate ccpoint center = CCP (winsize. width/2, winsize. height/2); // create the genie ccsprite * sprite = ccsprite: Create ("labelatlasimg.png"); // use the genie to create the progress bar ccprogresstimer * progress = ccprogresstimer :: create (sprite); addchild (Progress); // set the progress bar position progress-> setposition (center ); // set the progress bar mode // kccprogresstimertypebar indicates the bar mode progress-> settype (kccprogresstimertypebar); // set the direction of the progress bar change // setmidpoint is left by default // CCP) indicates the right change progress-> setbarchangerate (CCP (); // from which the change starts // CCP) indicates that progress-> setmidpoint (CCP (100) is changed from the beginning on the left. // you can set the progress ID to 100 progress-> settag ); // define a timer schedule (schedule_selector (progressttor: schedulefunc), 0.1f); Return true;} void progresstimer: schedulefunc (float DT) {// get the progress bar ccprogresstimer * progress = (ccprogresstimer *) getchildbytag (100); Progress-> setpercentage (Progress-> getpercentage () + 1) through the progress bar ID ); if (Progress-> getpercentage () >=100) {unscheduleallselectors ();}}

Running result:


Progress bar 3

First, add two progress bars to the resource folder in the project folder.

Program code:

# Include "progresstimer. H "ccscene * progresstimer: Scene () {ccscene * scene = ccscene: Create (); progresstimer * layer = progresstimer: Create (); scene-> addchild (layer); Return scene;} bool progresstimer: Init () {// initialize the parent class cclayer: Init (); // obtain the size of the Form. ccsize winsize = ccdirector: shareddire()-> getwinsize (); // set the coordinate ccpoint center = CCP (winsize. width/2, winsize. height/2); // create the sprite ccsprite * BG = ccsprite: Create ("slidertrack.png"); ccsprite * sprite = ccsprite: Create ("sliderprogress.png "); // set the sprite's position BG-> setposition (center); // Add the genie addchild (BG); // use the genie to create the progress bar ccprogresstimer * progress = ccprogresstimer :: create (sprite); // set the progress bar position progress-> setposition (center); // Add the progress bar addchild (Progress ); // set the progress bar mode to the bar mode progress-> settype (kccprogresstimertypebar); // change progress-> setbarchangerate (CCP (1, 0) to the right )); // start from the left and change progress-> setmidpoint (CCP (0, 0); // set the tagprogress-> settag (100) of progress ); // Add a timer schedule (schedule_selector (progresstimer: schedulefunc), 0.1f); Return true;} void progresstimer: schedulefunc (float DT) {// get the progress bar ccprogresstimer * progress = (ccprogresstimer *) getchildbytag (100) through the progress bar ID; // set the progress of SS, each call progress is added with progress-> setpercentage (Progress-> getpercentage () + 1); // If (Progress-> getpercentage ()> = 100) {// terminate the timer unscheduleallselectors ();}}

Running result:


Progresstimer progress bar 4

Program code

# Include "progresstimer. H "ccscene * progresstimer: Scene () {ccscene * scene = ccscene: Create (); progresstimer * layer = progresstimer: Create (); scene-> addchild (layer); Return scene;} bool progresstimer: Init () {// initialize the parent class cclayer: Init (); // obtain the size of the Form. ccsize winsize = ccdirector: shareddire()-> getwinsize (); // set the coordinate ccpoint center = CCP (winsize. width/2, winsize. height/2); // create the sprite ccsprite * BG = ccsprite: Create ("slidertrack.png"); ccsprite * sprite = ccsprite: Create ("sliderprogress.png "); // set the sprite's position BG-> setposition (center); // Add the genie addchild (BG); // use the genie to create the progress bar ccprogresstimer * progress = ccprogresstimer :: create (sprite); // set the progress bar position progress-> setposition (center); // Add the progress bar addchild (Progress ); // set the progress bar mode to the bar mode progress-> settype (kccprogresstimertypebar); // change progress-> setbarchangerate (CCP (1, 0) to the right )); // set the tagprogress-> settag (100) of SS; // Add a timer schedule (schedule_selector (progressttor: schedulefunc), 0.1f); Return true;} void progresstimer :: schedulefunc (float DT) {// get the progress bar ccprogresstimer * progress = (ccprogresstimer *) getchildbytag (100) through the progress bar ID; // set the progress of SS, each call progress is added with progress-> setpercentage (Progress-> getpercentage () + 1); // If (Progress-> getpercentage ()> = 100) {// terminate the timer unscheduleallselectors ();}}

Running result:


Progress bar 5

Program code

# Include "progresstimer. H "ccscene * progresstimer: Scene () {ccscene * scene = ccscene: Create (); progresstimer * layer = progresstimer: Create (); scene-> addchild (layer); Return scene;} bool progresstimer: Init () {// initialize the parent class cclayer: Init (); // obtain the size of the Form. ccsize winsize = ccdirector: shareddire()-> getwinsize (); // set the coordinate ccpoint center = CCP (winsize. width/2, winsize. height/2); // create the sprite ccsprite * BG = ccsprite: Create ("slidertrack.png"); ccsprite * sprite = ccsprite: Create ("sliderprogress.png "); // set the genie's position BG-> setposition (center); // Add the genie addchild (BG); // The genie rotates 90 degrees BG-> setrotation (90 ); // use the Wizard to create the progress bar ccprogresstimer * progress = ccprogresstimer: Create (sprite); // set the progress bar position progress-> setposition (center ); // Add the progress bar addchild (Progress); // The progress bar rotates for 90 degrees SS-> setrotation (90); // set the progress bar mode to bar mode progress-> settype (kccprogresstimertypebar ); // change progress to the right-> setbarchangerate (CCP (1, 0); // change progress from the left-side Navigation Pane-> setmidpoint (CCP (1, 0 )); // set idprogress-> settag (100) of SS; // Add a timer schedule (schedule_selector (progressttor: schedulefunc), 0.1f); Return true;} void progresstimer :: schedulefunc (float DT) {// get the progress bar ccprogresstimer * progress = (ccprogresstimer *) getchildbytag (100) through the progress bar ID; // set the progress of SS, each call progress is added with progress-> setpercentage (Progress-> getpercentage () + 1); // If (Progress-> getpercentage ()> = 100) {// terminate the timer unscheduleallselectors ();}}

Running result:


Progressto implementation progress bar 1

Program code

# Include "progresstimer. H "ccscene * progresstimer: Scene () {ccscene * scene = ccscene: Create (); progresstimer * layer = progresstimer: Create (); scene-> addchild (layer); Return scene;} bool progresstimer: Init () {cclayer: Init (); ccsize winsize = ccdirector: shareddire () -> getwinsize (); ccpoint center = CCP (winsize. width/2, winsize. height/2); ccsprite * BG = ccsprite: Create ("slidertrack.png"); ccsprite * sprite = ccsprite: Create ("sliderprogress.png "); BG-> setposition (center); addchild (BG); // The genie rotates 90 degrees BG-> setrotation (90); // use the Wizard to create a progress bar ccprogresstation * progress = ccprogresstimer :: create (sprite); // set the progress bar position progress-> setposition (center); // Add the progress bar addchild (Progress ); // progress bar rotate for 90 degrees SS-> setrotation (90); // set the progress bar mode to bar mode progress-> settype (kccprogresstimertypebar ); // change progress to the right-> setbarchangerate (CCP (1, 0); // change progress from the left-side Navigation Pane-> setmidpoint (CCP (1, 0 )); // set idprogress-> settag (100) of progress; // this parameter is used to display the progress of progress in an animation. // The First Progress number: Time // The Second Progress Number: progress ccprogressto * progressto = ccprogressto: Create (2,100); // run progress-> runaction (progressto); Return true ;}

Running result:

Zookeeper

Zookeeper

Progresstimer in Cocos2d-X

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.