ProgressTimer in Cocos2d-X
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 directory.
Then create a ProgressTimer class
Add the following code in 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 in 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 window size CCSize winSize = CCDirector: sharedDirector ()-> getWinSize (); // set the coordinates 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 (50% of the progress bar is displayed) progress-> setPercentage (50); return true ;}
Execution 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 window size CCSize winSize = CCDirector: sharedDirector ()-> getWinSize (); // set the coordinates 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 to change progress-> setMidpoint (ccp (50%) from the left; // sets the percentage (of the progress bar is displayed) progress-> setPercentage (50); return true ;}
Execution 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 window size CCSize winSize = CCDirector: sharedDirector ()-> getWinSize (); // set the coordinates 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 ();}}
Execution 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 window size CCSize winSize = CCDirector: sharedDirector ()-> getWinSize (); // set the coordinates 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 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 ();}}
Execution result:
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + UHJvZ3Jlc3NUaW1lcsq1z9a9 + LbIzPUzPC9wPgo8cD7K18/I1Nq5pLPMxL/CvM/5E + fingerprint = "http://www.2cto.com/uploadfile/Collfiles/20141018/201410180912156.png" alt = "\">
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 window size CCSize winSize = CCDirector: sharedDirector ()-> getWinSize (); // set the coordinates 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 )); // change progress-> setMidpoint (ccp (0, 0) from the left; // set the tagprogress-> setTag (100) of progress ); // 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 ();}}
Execution 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 window size CCSize winSize = CCDirector: sharedDirector ()-> getWinSize (); // set the coordinates 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 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 ();}}
Execution 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 window size CCSize winSize = CCDirector: sharedDirector ()-> getWinSize (); // set the coordinates 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 left-> 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 ();}}
Execution 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 left-> 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 parameter is time // The second parameter is Progress CCProgressTo * progressTo = CCProgressTo:: create (2,100); // execution progress-> runAction (progressTo); return true ;}
Execution result:
Zookeeper
Zookeeper