Cocos2d-x study notes (8) -- progress (progress bar)

Source: Internet
Author: User

Cocos2d-x Study Notes (7) -- progress (progress bar)
In some games, we can see the loading progress of the game, or some pie charts when the game collects scores. Next we will learn the progress bar.
I want to learn two classes this time:
1. CCProgressTo
2. CCProgressTimer
In the online api document of the cocos2d-x, we can see the inheritance relationship of CCProgressTo:

That is to say, CCProgressTo is actually a class for setting actions, but it only adds the attribute unique to the progress bar.
Step 1: Create a cocos2d-win32 application and name it progress
Step 2: Add the following classes in HelloWorldScene. h:
[Cpp]
Class SpriteDemo: public CCLayer
{
 
Public:
Virtual std: string title ();
Virtual void onEnter ();
 
Void backCallback (CCObject * pSender );
Void restartCallback (CCObject * pSender );
Void nextCallback (CCObject * pSender );

};
 
 
Class SpriteProgressToRadial: public SpriteDemo
{
Public:
Virtual void onEnter ();
Virtual std: string title ();
};
 
Class SpriteProgressToHorizontal: public SpriteDemo
{
Public:
Virtual void onEnter ();
Virtual std: string title ();
};
 
Class SpriteProgressToVertical: public SpriteDemo
{
Public:
Virtual void onEnter ();
Virtual std: string title ();
};

Modify the scene function in HelloWorldScene. cpp:
[Cpp] view plaincopy
CCScene * HelloWorld: scene ()
{
CCScene * scene = NULL;
Do
{
// 'Scene 'is an autorelease object
Scene = CCScene: node ();
CC_BREAK_IF (! Scene );
 
// 'Player' is an autorelisted object
SpriteProgressToRadial * layer = new SpriteProgressToRadial ();

CC_BREAK_IF (! Layer );
 
// Add layer as a child to scene
Scene-> addChild (layer );
} While (0 );
 
// Return the scene
Return scene;
}

Add the following functions at the same time:
[Cpp]
/*************************************** *********************************/
/* SpriteDemo */
/*************************************** *********************************/
 
Std: string SpriteDemo: title ()
{
Return "ProgressActionsTest ";
}
 
 
Void SpriteDemo: onEnter ()
{
CCLayer: onEnter ();
 
CCSize size = CCDirector: shareddire()-> getWinSize ();
 
// Add a title
Std: string strTitle = title ();
Const char * ptitle = strTitle. c_str ();
CCLabelTTF * label = CCLabelTTF: labelWithString (ptitle, "Arial", 28 );
Label-> setPosition (ccp (size. width/2, size. height-30 ));
AddChild (label );
 
// Add basic button
CCLabelTTF * pNextLabel = CCLabelTTF: labelWithString ("Next", "Arial", 28 );
CCLabelTTF * pBackLabel = CCLabelTTF: labelWithString ("Back", "Arial", 28 );
CCLabelTTF * pRestartLabel = CCLabelTTF: labelWithString ("Restart", "Arial", 28 );
 
CCMenuItemLabel * pNextItem = CCMenuItemLabel: itemWithLabel (
PNextLabel, this, menu_selector (SpriteDemo: nextCallback ));
CCMenuItemLabel * pBackItem = CCMenuItemLabel: itemWithLabel (
PBackLabel, this, menu_selector (SpriteDemo: backCallback ));
CCMenuItemLabel * pRestartItem = CCMenuItemLabel: itemWithLabel (
PRestartLabel, this, menu_selector (SpriteDemo: restartCallback ));
 
CCMenu * pNextMenu = CCMenu: menuWithItem (pNextItem );
CCMenu * pBackMenu = CCMenu: menuWithItem (pBackItem );
CCMenu * pRestartMenu = CCMenu: menuWithItem (pRestartItem );
 
PNextItem-> setPosition (ccp (size. width/2 + 150, 50 ));
PBackItem-> setPosition (ccp (size. width/2-150, 50 ));
PRestartItem-> setPosition (ccp (size. width/2, 50 ));
 
PNextMenu-> setPosition (CCPointZero );
PBackMenu-> setPosition (CCPointZero );
PRestartMenu-> setPosition (CCPointZero );
 
AddChild (pNextMenu, 1 );
AddChild (pBackMenu, 1 );
AddChild (pRestartMenu, 1 );

}
 
 
Void SpriteDemo: backCallback (CCObject * pSender)
{
If (index = 1)
Return;
Index --;
CCScene * scene = new CCScene ();
CCLayer * layer = runThisTest (index );
Scene-> addChild (layer );
CCDirector: sharedDirector ()-> replaceScene (scene );
Scene-> release ();
Layer-> release ();

}
 
Void SpriteDemo: restartCallback (CCObject * pSender)
{
CCScene * scene = new CCScene ();
CCLayer * layer = runThisTest (index );
Scene-> addChild (layer );
CCDirector: sharedDirector ()-> replaceScene (scene );
Scene-> release ();
Layer-> release ();
}
 
 
Void SpriteDemo: nextCallback (CCObject * pSender)
{
If (index = MAX_INDEX)
Return;
Index ++;
CCScene * scene = new CCScene ();
CCLayer * layer = runThisTest (index );
Scene-> addChild (layer );
CCDirector: sharedDirector ()-> replaceScene (scene );
Scene-> release ();
Layer-> release ();
}
 
 
/*************************************** *********************************/
/* SpriteProgressToRadial */
/*************************************** *********************************/
 
Std: string SpriteProgressToRadial: title ()
{
Return "SpriteProgressToRadial ";
}
 
Void SpriteProgressToRadial: onEnter ()
{
SpriteDemo: onEnter ();
 
CCSize size = CCDirector: shareddire()-> getWinSize ();
 
CCProgressTo * p1 = CCProgressTo: actionWithDuration (2,80 );
// The first parameter is time, and the second parameter is to rotate only 80%
 
CCProgressTimer * pt = CCProgressTimer: progressWithFile ("radial.png ");
Pt-> setType (kCCProgressTimerTypeRadialCW );
AddChild (pt );
Pt-> setPosition (ccp (size. width/2, size. height/2 ));
Pt-> runAction (CCRepeatForever: actionWithAction (p1 ));
}
 
/*************************************** *********************************/
/* SpriteProgressToHorizontal */
/*************************************** *********************************/
 
Std: string SpriteProgressToHorizontal: title ()
{
Return "SpriteProgressToHorizontal ";
}
 
Void SpriteProgressToHorizontal: onEnter ()
{
SpriteDemo: onEnter ();
 
CCSize size = CCDirector: shareddire()-> getWinSize ();
 
CCProgressTo * to = CCProgressTo: actionWithDuration (2,100 );
CCProgressTimer * pt = CCProgressTimer: progressWithFile ("horizonal.png ");
Pt-> setType (kCCProgressTimerTypeHorizontalBarLR );
AddChild (pt );
Pt-> setPosition (ccp (size. width/2, size. height/2 ));
Pt-> runAction (CCRepeatForever: actionWithAction ());
}
 
 
/*************************************** *********************************/
/* SpriteProgressToVertical */
/*************************************** *********************************/
 
Std: string SpriteProgressToVertical: title ()
{
Return "SpriteProgressToVertical ";
}
 
Void SpriteProgressToVertical: onEnter ()
{
SpriteDemo: onEnter ();
 
CCSize size = CCDirector: shareddire()-> getWinSize ();
CCProgressTo * to = CCProgressTo: actionWithDuration (2,100 );
CCProgressTimer * pt = CCProgressTimer: progressWithFile ("vertical.png ");
Pt-> setType (kCCProgressTimerTypeVerticalBarBT );
Pt-> setPosition (ccp (size. width/2, size. height/2 ));
AddChild (pt );
Pt-> runAction (CCRepeatForever: actionWithAction ());
 
}

Step 3: run the program and see the following three effects:
Pie effect:

Horizontal Effect

 
Vertical effect:

Author: wen294299195

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.