cocos2d-x之CCProgressTimer实现进度条

CCProgressTimerType类型如下:

typedef enum {

    /// Radial Counter-Clockwise

    kCCProgressTimerTypeRadial,

    /// Bar

    kCCProgressTimerTypeBar,

} CCProgressTimerType;

实现从左到右的进度条:

     pt->setMidpoint(ccp(0,0));

        pt->setBarChangeRate(ccp(1,0));

具体实现如下:

1. 头文件,其中有2个私有数据成员,分别为进度条1和进度条2:

#include "cocos2d.h"

class LoadGame : public cocos2d::CCLayer
{
public:
    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();  

    // there's no 'id' in cpp, so we recommand to return the exactly class pointer
    static cocos2d::CCScene* scene();
    
    // a selector callback
    void menuCloseCallback(CCObject* pSender);

    void LoadGame::update(float dt);

    // implement the "static node()" method manually
    CREATE_FUNC(LoadGame);
    
private:
    cocos2d::CCProgressTimer *progresstime1;
    cocos2d::CCProgressTimer *progresstime2;
};

#endif  // __HELLOWORLD_SCENE_H__
2. LoadGame类具体实现

//创建二个精灵,一绿一红
        CCSprite *psSprite1 = CCSprite::create("green.png");
        CCSprite *psSprite2 = CCSprite::create("red.png");
        
        //利用精灵创建进度条,并设置一些属性
        progresstime1 = CCProgressTimer::create(psSprite1);    //初始化CCProgressTimer
        progresstime1->setPercentage(0.0f);    //设置初始百分比的值
        progresstime1->setScale(3);            //设置进度条大小为原始的3倍
        progresstime1->setBarChangeRate(ccp(1, 0));    //设置进度条的长度和高度开始变化的大小
        progresstime1->setType(kCCProgressTimerTypeBar);    //设置进度条为水平
        progresstime1->setPosition(ccp(size.width/2, size.height/2));    //放置进度条位置

        this->addChild(progresstime1, 100);    //加入Layer中

        //利用精灵创建进度条,并设置一些属性
        progresstime2 = CCProgressTimer::create(psSprite2);    //初始化CCProgressTimer
        progresstime2->setPercentage(100.0f);    //设置初始百分比的值
        progresstime2->setScale(3);            //设置进度条大小为原始的3倍
        progresstime2->setBarChangeRate(ccp(1, 0));    //设置进度条的长度和高度开始变化的大小
        progresstime2->setType(kCCProgressTimerTypeBar);    //设置进度条为水平
        progresstime2->setPosition(ccp(size.width/2, size.height/2 - 30));    //放置进度条位置

        this->addChild(progresstime2, 101);    //加入Layer中

        this->scheduleUpdate();        //调用定时器更新进度条
复制代码
 

3. 定时器的具体实现:

void LoadGame::update(float dt)
{
    //CCProgressTimer *progresstime = static_cast(this->getChildByTag(100));
    float ct1 = progresstime1->getPercentage();    //取得当前进度的百分比
    float ct2 = progresstime2->getPercentage();    //取得当前进度的百分比
    
    ct1 = ct1 + 0.5f;    //每帧+0.5%
    ct2 = ct2 - 0.5f;
    
    //如果进度条小于100%,设置进度条的百分比
    if (ct1 <= 100)    
    {
        CCLOG("progresstime1:%f, progresstime2:%f", ct1, ct2);
        progresstime1->setPercentage(ct1);
        progresstime2->setPercentage(ct2);
    }
    //如果进度条达到100%,则进入过渡场景,过渡场景会在2秒后进入主场景
    else
    {
        CCTransitionFade *tScene = CCTransitionFade::create(2, HelloWorld::scene(), ccWHITE);
        CCDirector::sharedDirector()->replaceScene(tScene);
    }
    
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值