cocos2d-x开发 之 使用slider滑动条做吃鸡中医疗包的数量选择

效果如下

以下是头文件


#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;


	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值