cocos2d-x进度条的实现

本文详细介绍了如何在cocos2d-x游戏引擎中实现一个进度条效果,通过实例代码和最终展示的截图,展示了从设计到实现的完整过程,帮助开发者掌握在2D游戏中创建动态进度显示的技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include"prograssbar.h"
#include "variable.h"

using namespace  cocos2d;


CCScene * prograssbar::scene()
{
    CCScene *scene = NULL;
 bool bRet =false;

 do 
 {
  scene =CCScene::create();
  prograssbar* layer =prograssbar::create();

  scene->addChild(layer);

  bRet =true;


 } while (0);

 return scene;
}

 

bool prograssbar::init()
{

 CCSize winsize =CCDirector::sharedDirector()->getWinSize();

 bool bRet =false;

 do{
 //创建一个进度条精灵,这个是2.0以后api变了
 CCSprite *progressbgSprite=CCSprite::create("prograssbar/lodingbg.png");

 progressbgSprite->setPosition(ccp(winsize.width/2.0, winsize.height/2.0));

 this->addChild(progressbgSprite, 1);


 CCSprite *progressSprite=CCSprite::create("prograssbar/longding.png");

 progress1=CCProgressTimer::create(progressSprite);

 progress1->setType(kCCProgressTimerTypeBar);

 progress1->setPosition(ccp(winsize.width/2.0, winsize.height/2.0));

 //进度动画运动方向,可以多试几个值,看看效果
 progress1->setMidpoint(ccp(0, 0));

 //进度条宽高变化
 progress1->setBarChangeRate(ccp(1, 0));

 progress1->setPercentage(0);

 this->addChild(progress1, 1);


 numsTTF=CCLabelTTF::create("0",ID_FONT_FORMAT, ID_FONT_SIZE);


 numsTTF->setPosition(ccp(winsize.width/2.0, winsize.height/2.0));


 this->addChild(numsTTF, 1);


 this->scheduleUpdate();

 bRet =true;
 }while(0);

 return bRet;

}

 

void prograssbar::update(float dt)
{
 float cu=progress1->getPercentage();


 if(cu<100)
 cu=cu+0.2f;


 if(cu<100)
 {

    progress1->setPercentage(cu);


    CCString *str = CCString::createWithFormat("%.2f%%",cu);

    numsTTF->setString(str->getCString());

 }

}

 

 

 

效果图如下


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值