GameOver.h

  1. #ifdef WIN32
  2. #pragma execution_character_set("utf-8")
  3. #pragma once
  4. #ifndef __GAMEOVER_SCENE_H__
  5. #define __GAMEOVER_SCENE_H__
  6. #include "cocos2d.h"
  7. #include "ui/CocosGUI.h"
  8. #include "SimpleAudioEngine.h"
  9. #include <string.h>
  10. #include "cocos-ext.h"
  11. using namespace cocos2d::ui;
  12. using namespace CocosDenshion;
  13. USING_NS_CC_EXT;
  14. USING_NS_CC;
  15. class GameOver : public cocos2d::Scene
  16. {
  17. public:
  18. static cocos2d::Scene* createScene();
  19. int historyScore = 0;
  20. int nowScore = 0;
  21. Label* scoreLabel;
  22. Label* historyLabel;
  23. virtual bool init();
  24. void showScoreInfo();
  25. void menuCloseCallback(cocos2d::Ref* pSender);
  26. void backToGameMenuScene(); //返回主菜单
  27. void setNowScore(int score);
  28. // implement the "static create()" method manually
  29. CREATE_FUNC(GameOver);
  30. };
  31. #endif // __GAMEOVER_SCENE_H__#pragma once
  32. #endif


GameOver.cpp

  1. #include "GameOver.h"
  2. USING_NS_CC_EXT;
  3. USING_NS_CC;
  4. Scene* GameOver::createScene() {
  5. return GameOver::create();
  6. }
  7. static void problemLoading(const char* filename) {
  8. printf("Error while loading: %s\n", filename);
  9. printf("Depending on how you compiled you might have to add 'Resources/' in front of filenames in Level_01Scene.cpp\n");
  10. }
  11. bool GameOver::init()
  12. {
  13. if (!Scene::init()) {
  14. return false;
  15. }
  16. auto visibleSize = Director::getInstance()->getVisibleSize();
  17. Vec2 origin = Director::getInstance()->getVisibleOrigin();
  18. //创建背景
  19. Sprite *backGround = Sprite::create("background.png");
  20. backGround->setAnchorPoint(Point(0.5, 0.5));
  21. backGround->setPosition(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y);
  22. backGround->setScale(1);
  23. this->addChild(backGround, 0);
  24. //创建游戏区域
  25. Sprite *gameArea = Sprite::create("gameArea.png");
  26. gameArea->setPosition(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y);
  27. gameArea->setOpacity(200);
  28. this->addChild(gameArea, 1);
  29. //创建得分
  30. scoreLabel = Label::createWithTTF("您本次得分为:", "fonts/TencentSans-W7.ttf", 30);
  31. scoreLabel->setColor(Color3B::WHITE);
  32. scoreLabel->setPosition(Vec2(origin.x + visibleSize.width / 2,
  33. origin.y + visibleSize.height / 2 + 100));
  34. this->addChild(scoreLabel, 1);
  35. //从cocos自带的文件保存中读取历史分数
  36. historyScore = CCUserDefault::sharedUserDefault()->getIntegerForKey("historyScore", 0);
  37. log("历史记录分为:%d", historyScore);
  38. historyLabel = Label::createWithTTF("", "fonts/TencentSans-W7.ttf", 30);
  39. historyLabel->setColor(Color3B::WHITE);
  40. historyLabel->setPosition(Vec2(origin.x + visibleSize.width / 2,
  41. origin.y + visibleSize.height / 2 - 75));
  42. this->addChild(historyLabel, 1);
  43. //创建游戏下方的菜单
  44. MenuItemFont::setFontName("BoLeHuaiShuti");
  45. MenuItemFont::setFontSize(48);
  46. auto backToMainScene = MenuItemFont::create("返回游戏主菜单", CC_CALLBACK_0(GameOver::backToGameMenuScene, this));
  47. backToMainScene->setColor(Color3B(255, 250, 250));
  48. backToMainScene->setPosition(visibleSize.width / 2, 100);
  49. auto bottomMenu = Menu::create(backToMainScene, NULL);
  50. bottomMenu->setPosition(Vec2::ZERO);
  51. this->addChild(bottomMenu, 5);
  52. return true;
  53. }
  54. // 返回原菜单
  55. void GameOver::backToGameMenuScene() {
  56. //auto scene = GameMenu::createScene();
  57. Director::getInstance()->popScene();
  58. }
  59. void GameOver::menuCloseCallback(Ref* pSender)
  60. {
  61. Director::getInstance()->end();
  62. }
  63. void GameOver::showScoreInfo() {
  64. char highScoreStr[64];
  65. if (this->nowScore > this->historyScore) {
  66. sprintf(highScoreStr, "恭喜您\n您超过了最高分\n当前最高得分:%d", this->nowScore);
  67. CCUserDefault::sharedUserDefault()->setIntegerForKey("historyScore", this->nowScore);
  68. CCUserDefault::sharedUserDefault()->flush();//刷新储存
  69. }
  70. else {
  71. sprintf(highScoreStr, "抱歉\n您并未超过最高分\n当前最高记录:%d", this->historyScore);
  72. }
  73. historyLabel->setString(highScoreStr);
  74. }
  75. void GameOver::setNowScore(int score) {
  76. this->nowScore = score;
  77. char scoreStr[64] = "";
  78. sprintf(scoreStr, "您本次得分:%d", this->nowScore);
  79. scoreLabel->setString(scoreStr);
  80. showScoreInfo();
  81. }