微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Cocos2d-X中使用ProgressTimer实现一些简单的效果

我在上一篇博客中介绍了Progresstimer的用法,这篇博客决定写一些使用Progress实现一些简单的效果

程序实例:Progresstimer实现效果1

首先在工程目录下的Resource文件夹中添加两张图片

然后定义一个Progress类

在Progress.h中添加下面的代码

?
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;
class Progress : public cclayer
{
:
static CCScene* scene();
bool init();
CREATE_FUNC(Progress);
void scheduleFunc( float dt);
};
#endif

在Progress.cpp中添加下面代码

19
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
"Progress.h"
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 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得到进度条
CCProgresstimer* progress = (CCProgresstimer*)getChildByTag( );
//设置progress的进度,每调用一次进度加一
progress->setPercentage(progress->getPercentage() + );
//当进度大于或者等于100时
if (progress->getPercentage() >= )
{
//终止定时器
unscheduleAllSelectors();
}
}

执行结果:


首先在工程目录下的Resource文件夹中添加两张图片

在Progress.cpp中添加下面的代码

 


CCScene* Progress::scene()
{
    CCScene* scene = CCScene::create();
 
    Progress* layer = Progress::create();
 
    scene->addChild(layer);
 
    returnscene;
}
 
bool Progress::init()
{
    cclayer::init();
     
    //设置背景颜色为白色
    cclayerColor* layer = cclayerColor::create(ccc4(255,255,255));  
    addChild(layer);   
 
 
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
 
    CCPoint center = ccp(winSize.width / 2,winSize.height / 2);
 
    CCSprite* bg = CCSprite::create("4.png");
    CCSprite* sprite = CCSprite::create("3.png");
 
    bg->setPosition(center);
 
    addChild(bg);
 
    CCProgresstimer* progress = CCProgresstimer::create(sprite);
 
    progress->setPosition(center);
 
    addChild(progress);
 
    //设置进度条的ID
    progress->setTag(100);
 
    //创建一个定时器
    schedule(schedule_selector(Progress::scheduleFunc),0.1f);
 
    returntrue;
}
 
voidProgress::scheduleFunc(floatdt)
{
    //通过进度条的ID得到进度条
    CCProgresstimer* progress = (CCProgresstimer*)getChildByTag(100);
 
   //设置progress的进度,每调用一次进度加一
    progress->setPercentage(progress->getPercentage() + 1);
     
     //当进度大于或者等于100时
    if(progress->getPercentage() >= 100)
    {
          //终止定时器
        unscheduleAllSelectors();
    }
}



执行结果:

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐