闲着想要实现一个功能。一个填充爱心的效果。
效果如下:
一个爱心填满动作。
为实现这个功能,学习了下CCProgressTimer , 顺便做个记录。
首先介绍几个相关链接:
dota道具冷却效果:http://blog.csdn.net/cen616899547/article/details/9084561
技能冷却效果:http://blog.csdn.net/z104207/article/details/8059098
CCProgressTimer相关讲解:http://blog.sina.com.cn/s/blog_73df887801019gnb.html
cocos2d-x学习笔记(3) CCProgressTimer倒计时条小记 http://my.eoe.cn/baimu/archive/3315.html
进度条准备工作解析CCProgressTimer类 http://www.zaojiahua.com/ccprogresstimer.html
好了。 进入正题。
CCProgressTimer是Cocos2d-x中用于显示进度的派生类。一般可用与加载进度条,道具冷却等效果。
具体也没其他内容,下面就通过上面的爱心填充源码讲解。
附带图片资源: (heart_A.png)(heart_B.png)
//创建二个精灵,一个白色边框,一个逐渐增加的红心
CCSprite *psSprite1 = CCSprite::create("heart_A.png");
CCSprite *psSprite2 = CCSprite::create("heart_B.png");
//加载动作,设置5秒加载到100%
CCProgressTo *progressTo = CCProgressTo::create(5, 100);
CCProgressTimer *heart_ = CCProgressTimer::create(psSprite2);
//设置类型
heart_->setType(kCCProgressTimerTypeBar);
//起始点. 从哪个方向开始变化
heart_->setMidpoint(ccp(0, 0));
//ccp(1, 0) 水平方向 ccp(0, 1)垂直方向
heart_->setBarChangeRate(ccp(0, 1));
psSprite1->setPosition(ccp(size.width/2, 500));
addChild(psSprite1);
heart_->setPosition(ccp(size.width/2, 500));
addChild(heart_);
heart_->runAction(CCRepeatForever::create(progressTo));
具体的操作在代码中的给出了。很简单。
另外,需要注意一下。
在2.0中CCProgressTimer的type比1.0中少了很多,1.0中有:
kCCProgressTimerTypeRadialCW 顺时针生成
kCCProgressTimerTypeRadialCCW 逆时针生成
kCCProgressTimerTypeHorizontalBarLR 从左到右生成
kCCProgressTimerTypeHorizontalBarRL 从右到左生成
kCCProgressTimerTypeVerticalBarBT 从下到上生成
kCCProgressTimerTypeVerticalBarTB 从上到下生成
而2.0中只保留了2个常用的。
typedef enum {
/// Radial Counter-Clockwise
kCCProgressTimerTypeRadial,
/// Bar
kCCProgressTimerTypeBar,
} CCProgressTimerType;
不过,如果要实现一些效果,比如从下到上加载。如我给出的例子程序一样。只要设置好对应的setMidpoint, setBarChangeRate即可。
//起始点. 从哪个方向开始变化
heart_->setMidpoint(ccp(0, 0));
//ccp(1, 0) 水平方向 ccp(0, 1)垂直方向
heart_->setBarChangeRate(ccp(0, 1));