cocos2dx CCProgressTimer 进度条计时器

1  创建项目工程

2 打开win32的项目。

在HelloWorldScene.h中添加方法。

1     void update(float dt);
2     void shwoProssgressBar();

在HelloWorldScene.cpp中添加方法。

 1 void HelloWorld::update(float dt){
 2     
 3 
 4     CCProgressTimer *timer = (CCProgressTimer *)this->getChildByTag(100);
 5     CCLabelTTF * numsTTF=(CCLabelTTF *)this->getChildByTag(101);
 6     float ct1 = timer->getPercentage();//取得当前进度的百分比
 7      ct1 = ct1 + 0.5f;    //每帧+0.5%
 8 
 9     
10     //如果进度条小于100%,设置进度条的百分比
11       
12     if (ct1 <= 100)
13         
14     {
15         
16         timer->setPercentage(ct1);
17                
18     }
19     CCString *str= CCString::createWithFormat("%.2f%%",ct1);
20     numsTTF->setString(str->getCString());
21 
22 }
23 
24 void HelloWorld::shwoProssgressBar(){
25     this->setTouchEnabled(true);
26     CCSize size=CCDirector::sharedDirector()->getWinSize();
27     
28     CCSprite *progressSprite=CCSprite::create("progress.png");
29     CCProgressTimer *timer=CCProgressTimer::create(progressSprite);
30     timer->setType(kCCProgressTimerTypeBar);  //设置进度条为水平
31    
32    // timer->setPercentage(0.0f);    //设置初始百分比的值
33     timer->setPercentage(0.0f);   
34     timer->setScale(0.5);          
35     
36     //timer->setMidpoint(ccp(0,0));//进度动画运动方向,可以多试几个值,看看效果
37     //timer->setMidpoint(ccp(1,0));//进度动画运动方向<---
38     timer->setMidpoint(ccp(0,1));//进度动画运动方向--->
39 
40     timer->setBarChangeRate(ccp(1, 0));    //进度条从左到右
41     //timer->setBarChangeRate(ccp(0, 1));    //进度条高度从低到高.感觉像一个方向向量
42 
43     timer->setPosition(ccp(size.width/2-40, size.height-20));    //放置进度条位置
44     this->addChild(timer,1,100);
45     
46     CCLabelTTF * numsTTF=CCLabelTTF::create("0", "Thonburi", 18);
47     numsTTF->setPosition(ccp(size.width/2-20, size.height-20));
48     
49     this->addChild(numsTTF,1,101);
50 
51     
52     this->scheduleUpdate();        //调用定时器更新进度条
53 }


bool HelloWorld::init()方法里面添加shwoProssgressBar();

 

里面的方法说明:

转载于:https://www.cnblogs.com/aosting/p/3498461.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值