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

cocos2dx3.2利用ProgressTimer组合成评分控件,第1张

概述一、制作背景 现在很多游戏或者应用需要评分,就是一般来说满分10分,一般用星星来表示。 那么cocos2dx里面如何制作评分这样的控件呢? 我的打算是进度条组合成就行了。 二、材料准备 如上图所示,即2颗小星星即可。 三、最终效果 四、代码实现 ///////////////////////////////////////////////////////////////////////////// 一、制作背景 现在很多游戏或者应用需要评分,就是一般来说满分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;}
总结

以上是内存溢出为你收集整理的cocos2dx3.2利用ProgressTimer组合成评分控件全部内容,希望文章能够帮你解决cocos2dx3.2利用ProgressTimer组合成评分控件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1007202.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存