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

前端之家收集整理的这篇文章主要介绍了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更方便一些。


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

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

猜你在找的Cocos2d-x相关文章