I. background creation many games or applications now need to be scored, that is, a full score of 10 is generally expressed by stars. So how to make a score control in cocos2dx? My plan is to combine progress bars to achieve the goal. Ii. Material Preparation
As shown in, there are two stars. Iii. Final Results
Iv. Code Implementation
//////////////////////////////////////////////////////////////////////////////////***************************ScoreBar Class**************************************//////////////////////////////////////////////////////////////////////////////////class ScoreBar:public cocos2d::Node{public:CREATE_FUNC(ScoreBar);static ScoreBar* create(float mPercentage);CC_CONSTRUCTOR_ACCESS:ScoreBar();~ScoreBar();virtualbool 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' starunsigned char perFull = 100/elementCount;//per star full percentunsigned int per = mPercentage/perFull;// full star numunsigned char remain = (mPercentage%perFull)*elementCount;//the remain percentfor(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 combines the progresstimer into a scoring Control