效果如下
以下是头文件
#pragma once
#include "cocos2d.h"
using namespace cocos2d;
#include "ui/CocosGUI.h"
using namespace ui;
class SliderScene : public Scene
{
public:
//static SliderScene* getinstance();
static Scene* createscene();
virtual bool init();
CREATE_FUNC(SliderScene);
private:
SliderScene() :MaxPercent(100)//初始化为100
{
};
int MinPercent; //变化数量
const int MaxPercent; //最大数量限度
Label* _label; //显示数量
static SliderScene* GTI; //单例(可以按正常的,不需要单例)
};
以下是cpp文件
SliderScene* SliderScene::GTI = NULL;
Scene* SliderScene::createscene()
{
if (GTI == NULL)
{
GTI = new SliderScene;
return GTI->create();
}
else
{
return GTI->create();
}
}
bool SliderScene::init()
{
if (!Scene::init())
return false;
//进行创建
auto slider = Slider::create();
this->addChild(slider);
//设置百分比初始值
MinPercent = 0;
//设置文本
String* _str = String::createWithFormat("%d/%d", MinPercent, MaxPercent);
_label = Label::createWithSystemFont(_str->getCString(), "fonts/arial.ttf", 70);
_label->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
_label->setPosition(Director::getInstance()->getVisibleSize().width /2 + 100, Director::getInstance()->getVisibleSize().height /2 + 100);
this->addChild(_label);
//加载滑动条背景
slider->loadBarTexture("slider2.png");
//加载滑块资源
slider->loadSlidBallTextures("slider3.png", "slider3.png", "slider3.png");
//加载滑动条资源
slider->loadProgressBarTexture("slider1.png");
//slider->loadProgressBarTexture();
//设置滑动条百分比
slider->setPercent(MinPercent);
//设置动态事件
slider->addEventListener([&](Ref* ref, Slider::EventType type)
{
if (type == Slider::EventType::ON_PERCENTAGE_CHANGED)
{
int percent = dynamic_cast<Slider*> (ref)->getPercent();
MinPercent = percent;
String* str = String::createWithFormat("%d/%d", MinPercent, MaxPercent);
_label->setString(str->getCString());
}
});
//设置坐标
slider->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
slider->setPosition(Director::getInstance()->getVisibleSize() / 2);
//加载医疗包精灵
auto jijiubao = Sprite::create("timg.png");
this->addChild(jijiubao);
jijiubao->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
jijiubao->setPosition(Director::getInstance()->getVisibleSize()/2 + Size(0,250));
return true;
}