In game development, we usually need to make a game progress bar to transition the game's scene switch or display the game resources loading progress.
In the same way, the progress bar can be used as a life bar in the role-playing game as a player or a monster, or to eliminate the countdown to casual games. The progress bar is varied and how to use it properly depends on the game's project needs.
in the COCOS2DX , the progress bar uses Progresstimer To implement, first define the wizard binding progress bar.
Open the project we created Cocosxuexi , directly rewrite Init () the code in the initialization is implemented, and the previously written code is commented out first.
1. First, define 2 pointers in the HelloWorldScene.h file .
Public :
Virtual void Update (float dt); //Default scheduler
Private :
Progresstimer * PROGRESS1; //define progress bar pointer
Labelttf * NUMSTTF; //define label pointer
2. Create a progress bar and a progress bar border in the init () method in HelloWorldScene.cpp
Size visiblesize = Director :: getinstance ()->getvisiblesize ();
VEC2 origin = Director :: getinstance ()->getvisibleorigin ();
Create a progress bar sprite border
progressbgsprite= ::create ( "Loadkuang.png" Span style= "font-family: ' New song body '; Background-color:rgb (255,255,255); font-size:13px;" >);
progressbgsprite->setposition (Vec2(origin.x + VISIBLESIZE.WIDTH/2,ORIGIN.Y + VISIBLESIZE.HEIGHT/2));
This ->addchild (Progressbgsprite, 1);
Create a progress bar wizard
Auto progresssprite= Sprite :: Create ("Loadbar.png");
progress1= Progresstimer :: Create (Progresssprite);
Progress1->settype (Kccprogresstimertypebar);
progress1->setposition (Vec2(origin.x + VISIBLESIZE.WIDTH/2,ORIGIN.Y + visiblesize.height/ 2));
Progress animation direction of motion, you can try several values to see the effect
progress1->setmidpoint (Vec2(0, 0));
Progress bar width High change
progress1->setbarchangerate (Vec2(1, 0));
progress1->setpercentage (0); //Set the progress bar percentage to 0%
This ->addchild (PROGRESS1, 1);
numsttf= Cclabelttf :: Create ("0", "Thonburi", 18);
Numsttf->setposition (origin.x + visiblesize.width/2,origin.y + VISIBLESIZE.HEIGHT/2);
This ->addchild (Numsttf, 1);
scheduleupdate (); //Default scheduler , calling the Update () method
3. Implement the update () method in HelloWordScene.cpp
void HelloWorld :: Update (floatdt)
{
float Cu=progress1->getpercentage (); //Get percentage
cu=cu+1.0f; //progress bar progress every time you add 1%
Progress1->setpercentage (CU);
if (cu <=) //If progress is less than or equal to 100%
{
Auto str1 = String :: Createwithformat ("%.2f%%", Cu);
numsttf->setstring (str1->getcstring ()); //Set label display content
}
If the progress bar reaches 100%, stop
Else
{
unscheduleupdate (); //stop default scheduler
}
}
to this, a usable progress bar has been implemented. If it is used to automatically switch the scene, the automatic switching operation is implemented directly after stopping the scheduler in Update ().
Effect preview:
650) this.width=650; "Title=" Click to view the original "border=" 0 "alt=" study note 12: Figure 1.gif "src=" http://www.byjth.com/content/uploadfile/ 201509/18651443592750.gif "width=" 766 "height=" 603 "/>
COCOS2DX Learning Notes 12:COCOS2DX progress bar (Progresstimer)
Http://www.byjth.com/biji/32.html
This article is from the "Close your eyes on the Dark" blog, please be sure to keep this source http://byjth.blog.51cto.com/4127898/1758984
COCOS2DX Learning Notes 12:COCOS2DX progress bar (Progresstimer)