1、LoadingBar的实现示例:
@H_502_13@#include "ui/UILoadingBar.h"
using namespace ui;
bool LoadingBarTest::init()
{
Sprite* bg_sprite=Sprite::create("HpBar/sliderBg.png");
bg_sprite->cocos2d::Node::setPosition(Point(200,200));
this->addChild(bg_sprite);
LoadingBar* loadingBar=LoadingBar::create("HpBar/sliderValue.png");
loadingBar->setPosition(Point(200,200));
loadingBar->setTag(0);
this->addChild(loadingBar);
scheduleUpdate();
return true;
}
voidLoadingBarTest::update(float dt){
count++;
if (count>100) {
count=0;
}
LoadingBar* loadingBar=static_cast<LoadingBar*>(this->getChildByTag(0));
loadingBar->setPercent(count);
}
2、ProgressTimer的实现示例:
bool ProgressTimerTest::init()
{
Sprite* bg_sprite = Sprite::create("HpBar/sliderBg.png");//设置背景图
Sprite* hp_sprite = Sprite::create("HpBar/sliderValue.png");
ProgressTimer* progressTimer=ProgressTimer::create(hp_sprite);
bg_sprite->setPosition(200,200);
this->addChild(bg_sprite);
progressTimer->setPosition(200,200);
progressTimer->setTag(0);
//progressTimer->setPercentage(0);
progressTimer->setType(ProgressTimer::Type::BAR);
progressTimer->setMidpoint(Point(0,0.5));
progressTimer->setBarChangeRate(Point(1,0));
this->addChild(progressTimer,2);
return true;
}
void ProgressTimerTest::update(float dt){
count++;
if (count>100) {
count=0;
}
ProgressTimer* progressTimer=static_cast<ProgressTimer*>(this->getChildByTag(0));
progressTimer->setPercentage(count);
}
3、Slider的实现示例:
@H_502_13@#include "extensions/GUI/CCControlExtension/CCControlSlider.h"
using namespace extension;
bool SliderTest::init()
{
ControlSlider* slider=ControlSlider::create("HpBar/sliderBg.png","HpBar/sliderValue.png",27)">"HpBar/sliderThumb.png"); //sliderThumb.png为空的图象文件
slider->setMinimumValue(0);
slider->setMaximumValue(100);
slider->setValue(0);
slider->setTag(0);
slider->setPosition(200,200);
this->addChild(slider);
scheduleUpdate();
return true;
}
voidSliderTest::update(float dt){
count++;
if (count>100) {
count=0;
}
ControlSlider* slider =static_cast<ControlSlider*>(this->getChildByTag(0));
slider->setValue(count);
}
以上三种控件都可实现相同的血量条效果,个人认为slider更方便一些。