GameOver.h
#ifdef WIN32
#pragma execution_character_set("utf-8")
#pragma once
#ifndef __GAMEOVER_SCENE_H__
#define __GAMEOVER_SCENE_H__
#include "cocos2d.h"
#include "ui/CocosGUI.h"
#include "SimpleAudioEngine.h"
#include <string.h>
#include "cocos-ext.h"
using namespace cocos2d::ui;
using namespace CocosDenshion;
USING_NS_CC_EXT;
USING_NS_CC;
class GameOver : public cocos2d::Scene
{
public:
static cocos2d::Scene* createScene();
int historyScore = 0;
int nowScore = 0;
Label* scoreLabel;
Label* historyLabel;
virtual bool init();
void showScoreInfo();
void menuCloseCallback(cocos2d::Ref* pSender);
void backToGameMenuScene(); //返回主菜单
void setNowScore(int score);
// implement the "static create()" method manually
CREATE_FUNC(GameOver);
};
#endif // __GAMEOVER_SCENE_H__#pragma once
#endif
GameOver.cpp
#include "GameOver.h"
USING_NS_CC_EXT;
USING_NS_CC;
Scene* GameOver::createScene() {
return GameOver::create();
}
static void problemLoading(const char* filename) {
printf("Error while loading: %s\n", filename);
printf("Depending on how you compiled you might have to add 'Resources/' in front of filenames in Level_01Scene.cpp\n");
}
bool GameOver::init()
{
if (!Scene::init()) {
return false;
}
auto visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
//创建背景
Sprite *backGround = Sprite::create("background.png");
backGround->setAnchorPoint(Point(0.5, 0.5));
backGround->setPosition(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y);
backGround->setScale(1);
this->addChild(backGround, 0);
//创建游戏区域
Sprite *gameArea = Sprite::create("gameArea.png");
gameArea->setPosition(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y);
gameArea->setOpacity(200);
this->addChild(gameArea, 1);
//创建得分
scoreLabel = Label::createWithTTF("您本次得分为:", "fonts/TencentSans-W7.ttf", 30);
scoreLabel->setColor(Color3B::WHITE);
scoreLabel->setPosition(Vec2(origin.x + visibleSize.width / 2,
origin.y + visibleSize.height / 2 + 100));
this->addChild(scoreLabel, 1);
//从cocos自带的文件保存中读取历史分数
historyScore = CCUserDefault::sharedUserDefault()->getIntegerForKey("historyScore", 0);
log("历史记录分为:%d", historyScore);
historyLabel = Label::createWithTTF("", "fonts/TencentSans-W7.ttf", 30);
historyLabel->setColor(Color3B::WHITE);
historyLabel->setPosition(Vec2(origin.x + visibleSize.width / 2,
origin.y + visibleSize.height / 2 - 75));
this->addChild(historyLabel, 1);
//创建游戏下方的菜单
MenuItemFont::setFontName("BoLeHuaiShuti");
MenuItemFont::setFontSize(48);
auto backToMainScene = MenuItemFont::create("返回游戏主菜单", CC_CALLBACK_0(GameOver::backToGameMenuScene, this));
backToMainScene->setColor(Color3B(255, 250, 250));
backToMainScene->setPosition(visibleSize.width / 2, 100);
auto bottomMenu = Menu::create(backToMainScene, NULL);
bottomMenu->setPosition(Vec2::ZERO);
this->addChild(bottomMenu, 5);
return true;
}
// 返回原菜单
void GameOver::backToGameMenuScene() {
//auto scene = GameMenu::createScene();
Director::getInstance()->popScene();
}
void GameOver::menuCloseCallback(Ref* pSender)
{
Director::getInstance()->end();
}
void GameOver::showScoreInfo() {
char highScoreStr[64];
if (this->nowScore > this->historyScore) {
sprintf(highScoreStr, "恭喜您\n您超过了最高分\n当前最高得分:%d", this->nowScore);
CCUserDefault::sharedUserDefault()->setIntegerForKey("historyScore", this->nowScore);
CCUserDefault::sharedUserDefault()->flush();//刷新储存
}
else {
sprintf(highScoreStr, "抱歉\n您并未超过最高分\n当前最高记录:%d", this->historyScore);
}
historyLabel->setString(highScoreStr);
}
void GameOver::setNowScore(int score) {
this->nowScore = score;
char scoreStr[64] = "";
sprintf(scoreStr, "您本次得分:%d", this->nowScore);
scoreLabel->setString(scoreStr);
showScoreInfo();
}