Cocos2d-x CCProgressTimer

Cocos2d-x CCProgressTimer

CCProgressTimer,创建使用这个节点可以大致实现两个作用的效果:

其一:在游戏中几乎大部分的游戏启动界面都是游戏加载画面,那么用到的一般是进度条提示加载进度,其使用的就是CCProgressTimer。

其二:在游戏中需要对精灵的出现等动作制作一些渐显的效果。

 

(1)类型一般就是两种:

 

  1. typedef enum {  
  2.     /// Radial Counter-Clockwise  
  3.     kCCProgressTimerTypeRadial,  
  4.     /// Bar  
  5.     kCCProgressTimerTypeBar,  
  6. } CCProgressTimerType;  


(2)类型1:radial(环形)

 

 

  1. CCSize wSize = CCDirector::sharedDirector()->getWinSize();  
  2.     progressTimer = CCProgressTimer::create(CCSprite::create("progress.gif"));  
  3.     progressTimer->setType(kCCProgressTimerTypeRadial);  
  4.     // 默认的情况下,环形渐变的方向是:顺时针  
  5.     // 改变其渐变的方向 Makes the ridial CCW (逆时针)  
  6.     progressTimer->setReverseProgress(true);  
  7.     progressTimer->setPosition(wSize.width/2,wSize.height/2);  
  8.     this->addChild(progressTimer);  

 


(3)类型2:bar  (条形:包括vertical 和 horizontal)

渐变的方向问题:

vertical竖直方法包括从上到下和从下到上;

horizontal水平方向包括从左到右和从右到左。

这里涉及到两个设置参数:

首先是setMidpoint设置起点

 

  1. /** 
  2.      *    Midpoint is used to modify the progress start position. 
  3.      *    If you're using radials type then the midpoint changes the center point 
  4.      *    If you're using bar type the the midpoint changes the bar growth 
  5.      *        it expands from the center but clamps to the sprites edge so: 
  6.      *        you want a left to right then set the midpoint all the way to ccp(0,y) 
  7.      *        you want a right to left then set the midpoint all the way to ccp(1,y) 
  8.      *        you want a bottom to top then set the midpoint all the way to ccp(x,0) 
  9.      *        you want a top to bottom then set the midpoint all the way to ccp(x,1) 
  10.      */  


其次是setBarChangeRate设置变化rate

 

 

  1. /** 
  2.      *    This allows the bar type to move the component at a specific rate 
  3.      *    Set the component to 0 to make sure it stays at 100%. 
  4.      *    For example you want a left to right bar but not have the height stay 100% 
  5.      *    Set the rate to be ccp(0,1); and set the midpoint to = ccp(0,.5f); 
  6.      */  


如果不用变化的方向,则设置该方向为0,否则设置为1。

 

 

  1. CCSize wSize = CCDirector::sharedDirector()->getWinSize();  
  2.     progressTimer = CCProgressTimer::create(CCSprite::create("progress.gif"));  
  3.     progressTimer->setType(kCCProgressTimerTypeBar);  
  4.       
  5.     //从左到右  
  6.     progressTimer->setMidpoint(ccp(0, 0.5));  
  7.     progressTimer->setBarChangeRate(ccp(1, 0));  
  8.       
  9.     //从右到左  
  10. //    progressTimer->setMidpoint(ccp(1, 0.5));  
  11. //    progressTimer->setBarChangeRate(ccp(1, 0));  
  12.       
  13.     //从上到下  
  14. //    progressTimer->setMidpoint(ccp(0.5, 1));  
  15. //    progressTimer->setBarChangeRate(ccp(0, 1));  
  16.       
  17.     //从下到上  
  18. //    progressTimer->setMidpoint(ccp(0.5, 0));  
  19. //    progressTimer->setBarChangeRate(ccp(0, 1));  
  20.       
  21.     progressTimer->setPosition(wSize.width/2,wSize.height/2);  
  22.     this->addChild(progressTimer);  


(4) 执行变化

 

①、如果是要实现精灵渐变的显示效果:

创建CCProgressTo或者是CCProgressFromTo动作,让CCProgressTimer执行。
CCProgressTo和CCProgressFromTo的区别是:

前者:Progress to percentage(初始化有两个参数)(float duration, float fPercent)

后者:Progress from a percentage to another percentage(初始化有三个参数)(float duration, float fFromPercentage, float fToPercentage)

 

  1. CCProgressTo *progressTo = CCProgressTo::create(2.0, 100);  
  2.     //等价于:  
  3.     //CCProgressFromTo *progressFromTo = CCProgressFromTo::create(2.0, 0, 100);  
  4.     progressTimer->runAction(CCRepeatForever::create(progressTo));  


②、如果是要实现加载进度条的效果:

 

需要重载update方法,在这个方法中实现进度条percentage的变化。

 

  1. this->scheduleUpdate();  

 

  1. void HelloWorld::update(float dt)  
  2. {  
  3.     float percentage = progressTimer->getPercentage();  
  4.       
  5.     if (percentage < 100) {  
  6.         percentage += 1;  
  7.         progressTimer->setPercentage(percentage);  
  8.     }  
  9. }  

 

 

关于CCProgressTimer的更加详细的使用 demo可以参看引擎中sample中的ActionProgressTest。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值