在cocos2d-x 3.2中从左到右移动进度条

时间:2014-10-07 03:53:45

标签: c++ progress-bar cocos2d-x cocos2d-x-3.0

我使用以下代码在cocos2d-x 3.2中创建进度条。

Sprite *fuelBarBorder;

fuelBarBorder = CCSprite::create("ui_kbg.png");

fuelBarBorder->setPosition(_visibleSize.width/2,_visibleSize.height/2);

this->addChild(fuelBarBorder, 1);


CCProgressTimer *fuelBar;
fuelBar = CCProgressTimer::create( CCSprite::create("slt_bg.png"));

// Set this progress bar object as kCCProgressTimerTypeBar (%)
fuelBar->setType(ProgressTimerType::BAR);

// Set anchor point in 0,0 and add it as a child to our border sprite
fuelBar->setAnchorPoint(ccp(0, 0));

fuelBar->setBarChangeRate(ccp(1, 0)); // To make width 100% always
fuelBar->setTag(1);                  // Tag our object for easy access

fuelBarBorder->addChild(fuelBar, 50); // Add it inside the border sprite

问题是从中心向两个方向移动(即L和R),我想在LR方向移动它,我该怎么做?

2 个答案:

答案 0 :(得分:2)

使用fuelBar->setMidpoint(Vec2(0.0f, 0.1f));进行LR方向。

答案 1 :(得分:1)

我建议你看看cocos2d-x Gui Widget http://www.cocos2d-x.org/docs/cocos2d-x/en/ui_components/loading_bar.html

更简单的方法

编辑1: 头

#include "ui/CocosGUI.h"
using namespace cocos2d::ui;

在init

LoadingBar* loadingBar = LoadingBar::create();
loadingBar->setName("LoadingBar");
loadingBar->loadTexture("cocosgui/sliderProgress.png");
loadingBar->setPercent(0);
loadingBar->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f +     loadingBar->getSize().height / 4.0f));
m_pUiLayer->addChild(loadingBar);
this->schedule(schedule_selector(Scene::UpdateProgress),0.025f);

然后在回调函数

void Scene::UpdateProgress(float dt)
{
counter++;
if (counter > 100)
{
    this->scheduleOnce( schedule_selector( Scene::GoToScene ), 0 );

    unschedule(schedule_selector(Scene::UpdateProgress));
}
loadingBar->setPercent(counter);
}