cocos2d-x 血量条实现:LoadingBar、ProgressTimer和Slider实现示例

1、LoadingBar的实现示例:

先导入需要的库文件

#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的实现示例:


#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更方便一些。


使用的图片资源见下,另存为可使用
背景图
血量图

前面确实有个空白文件
右键保存使用吧!

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...