| 版权声明:本文为博主原创文章,未经博主允许不得转载。
这个类主要实现的是,显示历次成绩中的最好成绩。当然我写的这个很简洁,还可以写的更加的丰富。下面贴上代码:
GameScore.h
#ifndef _GAME_SCORE_H_ #define _GAME_SCORE_H_ #include "cocos2d.h" USING_NS_CC; class GameScore : public cocos2d::Layer { private: cocos2d::Sprite* background; cocos2d::MenuItemImage* awayItem; public: static cocos2d::Scene* createScene(); virtual bool init(); void scoreInterface(); void goMainInterface(cocos2d::Ref*); CREATE_FUNC(GameScore); }; #endif // _GAME_SCORE_H_
GameScore.cpp
#include "GameScore.h" #include "GameUnit.h" #include "MainMenu.h" #include "GameData.h" unit u2; cocos2d::Scene* GameScore::createScene() { Scene* scene = Scene::create(); GameScore* layer = GameScore::create(); scene->addChild(layer); return scene; } bool GameScore::init() { if (!Layer::init()) { return false; } this->scoreInterface(); return true; } void GameScore::scoreInterface() { background = Sprite::create("background/score.png"); background->setPosition(Vec2(u2.winOrigin().x + u2.winSize().width / 2, u2.winOrigin().y + u2.winSize().height / 2)); background->setScale(u2.scaleX(background, u2.winSize()), u2.scaleY(background, u2.winSize())); this->addChild(background, 0); awayItem = MenuItemImage::create( "button/away.png", "button/buttom.png", CC_CALLBACK_1(GameScore::goMainInterface, this)); awayItem->setPosition(Vec2(u2.winOrigin().x + awayItem->getContentSize().width / 2, u2.winOrigin().y + awayItem->getContentSize().height / 2)); auto m = Menu::create(awayItem, NULL); m->setPosition(Vec2::ZERO); this->addChild(m, 2); int bestScore = GameData::getGameData(); cocos2d::LabelTTF* score = LabelTTF::create("0", "Arial", 26); score->setPosition(Vec2(u2.winSize().width / 2, u2.winSize().height / 2)); this->addChild(score, 4); score->setColor(Color3B(0, 0, 0)); score->setString(__String::createWithFormat("%d : Pipe", bestScore - 1)->getCString()); } void GameScore::goMainInterface(cocos2d::Ref* pSender) { Director::getInstance()->replaceScene(TransitionFadeBL::create(1, MainMenu::createScene())); }
函数功能:http://www.cnblogs.com/geore/p/5800009.html
效果图: