我在上一篇博客中介绍了Progresstimer的用法,这篇博客决定写一些使用Progress实现一些简单的效果,
然后定义一个Progress类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#ifndef _Progress_H_
#define _Progress_H_
#include
"cocos2d.h"
USING_NS_CC;
{
:
static
CCScene* scene();
bool init();
CREATE_FUNC(Progress);
void
scheduleFunc(
float
dt);
};
#endif
|
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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);
progress->setPosition(center);
addChild(progress);
//设置进度条的模式
//kCCProgresstimerTypeBar表示条形模式
progress->setType(kCCProgresstimerTypeBar);
//设置进度条变化的方向
//ccp(0,1)表示沿着y轴变化
progress->setBarChangeRate(ccp(
0
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
1
));
//设置进度条的起始位置
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,0)表示下面
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得到进度条
//设置progress的进度,每调用一次进度加一
progress->setPercentage(progress->getPercentage() +
);
//当进度大于或者等于100时
if
(progress->getPercentage() >=
)
{
//终止定时器
unscheduleAllSelectors();
}
}