微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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


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

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

原文地址:https://www.jb51.cc/cocos2dx/341381.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐