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();
    }
}



执行结果:

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...