我在上一篇博客中介绍了ProgressTimer的用法,这篇博客决定写一些使用Progress实现一些简单的效果,
程序实例:ProgressTimer实现效果1
然后定义一个Progress类
1
2
3
4
5
6
7
8
9
10
@H_502_42@
11
12
13
14
15
16
17
18
19
|
#ifndef _Progress_H_
#define _Progress_H_
#include
"cocos2d.h"
USING_NS_CC;
class
Progress :
public
CCLayer
{
:
static
CCScene* scene();
bool init();
CREATE_FUNC(Progress);
void
scheduleFunc(
float
dt);
};
#endif
|
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
CCScene* Progress::scene()
{
CCScene* scene = CCScene::create();
Progress* layer = Progress::create();
scene->addChild(layer);
return
scene;
}
bool Progress::init()
{
CCLayer::init();
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCPoint center = ccp(winSize.width /
2
,winSize.height /
);
CCSprite* bg = CCSprite::create(
"2.png"
);
CCSprite* sprite = CCSprite::create(
"1.png"
);
bg->setPosition(center);
addChild(bg);
CCProgressTimer* progress = CCProgressTimer::create(sprite);
progress->setPosition(center);
addChild(progress);
//设置进度条的模式
//kCCProgressTimerTypeBar表示条形模式
progress->setType(kCCProgressTimerTypeBar);
//设置进度条变化的方向
//ccp(0,1)表示沿着y轴变化
progress->setBarChangeRate(ccp(
0
1
));
//设置进度条的起始位置
progress->setMidpoint(ccp(
));
//设置进度条的ID
progress->setTag(
100
);
//创建一个定时器
schedule(schedule_selector(Progress::scheduleFunc),monospace!important; font-size:1em!important; min-height:auto!important">.1f);
return
true
;
}
Progress::scheduleFunc(
dt)
{
//通过进度条的ID得到进度条
CCProgressTimer* progress = (CCProgressTimer*)getChildByTag(
);
//设置progress的进度,每调用一次进度加一
progress->setPercentage(progress->getPercentage() +
);
//当进度大于或者等于100时
if
(progress->getPercentage() >=
)
{
//终止定时器
unscheduleAllSelectors();
}
}