cocos2dx3.2利用ProgressTimer组合成评分控件

前端之家收集整理的这篇文章主要介绍了cocos2dx3.2利用ProgressTimer组合成评分控件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一、制作背景

现在很多游戏或者应用需要评分,就是一般来说满分10分,一般用星星来表示。
那么cocos2dx里面如何制作评分这样的控件呢?
我的打算是进度条组合成就行了。

二、材料准备


如上图所示,即2颗小星星即可。

三、最终效果



四、代码实现

/////////////////////////////////////////////////////////////////////////////////

/***************************scoreBar Class**************************************/

/////////////////////////////////////////////////////////////////////////////////

class scoreBar:public cocos2d::Node
{
public:
		CREATE_FUNC(scoreBar);
	static scoreBar* create(float mPercentage);
CC_CONSTRUCTOR_ACCESS:
		scoreBar();
		~scoreBar();
	virtual	bool init();
    virtual bool initSelf();
	virtual bool initBackGround();
	virtual bool initProgress();
public:
	void setPercent(unsigned char mPercentage);
    float getPercent() const;
private:
		cocos2d::Vector<cocos2d::ProgressTimer*>* _progressBars;  
		cocos2d::Node* _backGround;                                 
};

/////////////////////////////////////////////////////////////////////////////////

/***************************scoreBar Class**************************************/

/////////////////////////////////////////////////////////////////////////////////

scoreBar::scoreBar():_progressBars(nullptr),_backGround(nullptr)
{

}

scoreBar::~scoreBar()
{
	_progressBars->clear();
	CC_SAFE_DELETE(_progressBars);
	CC_SAFE_RELEASE(_backGround);
}

scoreBar* scoreBar::create( float mPercentage )
{
	auto bar = new scoreBar();
	if (bar&&bar->init())
	{
		bar->setPercent(mPercentage);
		bar->autorelease();
		return bar;
	}
	CC_SAFE_DELETE(bar);
	bar = nullptr;
	return nullptr;
}

bool scoreBar::init()
{
	bool ret = false;
	if (Node::init())
	{
		IF_RETURN_FALSE(!initSelf());
		return true;
	}
	return ret;
}

bool scoreBar::initSelf()
{
	IF_RETURN_FALSE(!initBackGround());
	IF_RETURN_FALSE(!initProgress());
	return true;
}

bool scoreBar::initBackGround()
{
	unsigned char elementCount = 5;
	const std::string elementBg  = "grid/star_default.png";
	_backGround = Node::create();
	IF_RETURN_FALSE(!_backGround);
	addChild(_backGround);
	for (unsigned char i=0;i<elementCount;i++)
	{
		auto elementSprite = Sprite::create(elementBg);
		IF_RETURN_FALSE(!elementSprite);
		_backGround->addChild(elementSprite);
		elementSprite->setPosition(elementSprite->getContentSize().width*i,0.0f);
	}
	return true;
}

bool scoreBar::initProgress()
{
	unsigned char elementCount = 5;
	const std::string elementPre =  "grid/star_show.png";
	float w = 0.0f;
	float h = 0.0f;
	if (nullptr==_progressBars)
	{
		_progressBars = new cocos2d::Vector<cocos2d::ProgressTimer*>();
	}
	for (unsigned char i=0;i<elementCount;i++)
	{
		auto elemntSprite = Sprite::create(elementPre);
		IF_RETURN_FALSE(!elemntSprite);
	
		w = elemntSprite->getContentSize().width;
		h = elemntSprite->getContentSize().height;
		auto mProgressBar = ProgressTimer::create(elemntSprite);
		IF_RETURN_FALSE(!mProgressBar);
		addChild(mProgressBar);
		_progressBars->pushBack(mProgressBar);
		mProgressBar->setPosition(elemntSprite->getContentSize().width*i,0.0f);
		mProgressBar->setType(ProgressTimer::Type::BAR);
		mProgressBar->setMidpoint(Vec2(0,0));
		mProgressBar->setBarChangeRate(Vec2(1,0));
	}	

	this->setContentSize(Size(w*elementCount,h));
	//CC_SAFE_RELEASE(baseSprite);
	return true;
}

void scoreBar::setPercent( unsigned char mPercentage )
{
	unsigned char elementCount = 5;								//the total num of  progress' star
	unsigned char perFull = 100/elementCount;					//per star full percent
	unsigned int per = mPercentage/perFull;						// full star num
	unsigned char remain = (mPercentage%perFull)*elementCount;	//the remain percent
	for(unsigned char i=0;i<_progressBars->size();i++)
	{
		if(i<per)
		_progressBars->at(i)->setPercentage(100);
		else
		_progressBars->at(i)->setPercentage(0);
	}
	if (per<_progressBars->size())
	{
		_progressBars->at(per)->setPercentage(remain);
	}
}

float scoreBar::getPercent() const
{
	float percent = 0;
	unsigned char elementCount = 5;
	for(auto e:*_progressBars)
	{
		percent+=e->getPercentage();
	}
	return percent/elementCount;
}
原文链接:https://www.f2er.com/cocos2dx/346537.html

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