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更方便一些。
使用的图片资源见下,另存为可使用
背景图
血量图
前面确实有个空白文件
右键保存使用吧!
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |