【cocos2d-x 3.7 飞机大战】 决战南海I (十二) 游戏结束场景

前端之家收集整理的这篇文章主要介绍了【cocos2d-x 3.7 飞机大战】 决战南海I (十二) 游戏结束场景前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

游戏结束的时候,要显示分数,还要能够选择是返回主场景还是退出游戏


  1. // 退出游戏
  2. void menuCloseCallback(cocos2d::Ref* pSender);
  3.  
  4. // 返回主界面
  5. void menuMainCallback(cocos2d::Ref* pSender);


实现该功能代码如下

  1. bool GameOver::init()
  2. {
  3. //////////////////////////////
  4. // 1. super init first
  5. if (!Layer::init())
  6. {
  7. return false;
  8. }
  9.  
  10. bool bRect = false;
  11.  
  12. //背景音乐
  13. if (CocosDenshion::SimpleAudioEngine::getInstance()->isBackgroundMusicPlaying())
  14. {
  15. CocosDenshion::SimpleAudioEngine::getInstance()->stopBackgroundMusic(true);
  16. CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic("sound/game_over.mp3",true);
  17. }
  18.  
  19. do
  20. {
  21. Size visibleSize = Director::getInstance()->getVisibleSize();
  22. Vec2 origin = Director::getInstance()->getVisibleOrigin();
  23.  
  24. //添加背景图片
  25. auto m_background = Sprite::createWithSpriteFrameName("backgroundGameOver.png");
  26. m_background->setPosition(Point(visibleSize.width / 2,visibleSize.height / 2));
  27. m_background->setAnchorPoint(Vec2(0.5,0.5));
  28. CC_BREAK_IF(!m_background);
  29.  
  30. this->addChild(m_background);
  31.  
  32. //添加分数
  33. auto score_int = UserDefault::getInstance()->getIntegerForKey("currentscore");
  34. auto score_str = __String::createWithFormat("%d",score_int);
  35. auto score = Label::createWithTTF(score_str->getCString(),"fonts/DFPShaoNvW5-GB.ttf",40);
  36. score->setPosition(Point(visibleSize.width / 2,visibleSize.height/3*2));
  37. score->setColor(Color3B(255,0));
  38. CC_BREAK_IF(!score);
  39.  
  40. this->addChild(score);
  41.  
  42. //设定等级
  43.  
  44. //设置标签获取中文文本
  45. auto dictionary = Dictionary::createWithContentsOfFile("fonts/AboutMe.xml");
  46. String rank_str;
  47.  
  48. switch (score_int/1000)
  49. {
  50. case 0:
  51. rank_str = ((__String*)(dictionary->objectForKey("Eleven")))->getCString();
  52. break;
  53. case 1:
  54. rank_str = ((__String*)(dictionary->objectForKey("Ten")))->getCString();
  55. break;
  56. case 2:
  57. rank_str = ((__String*)(dictionary->objectForKey("Nine")))->getCString();
  58. break;
  59. case 3:
  60. rank_str = ((__String*)(dictionary->objectForKey("Eight")))->getCString();
  61. break;
  62. case 4:
  63. rank_str = ((__String*)(dictionary->objectForKey("Seven")))->getCString();
  64. break;
  65. case 5:
  66. rank_str = ((__String*)(dictionary->objectForKey("Six")))->getCString();
  67. break;
  68. case 6:
  69. rank_str = ((__String*)(dictionary->objectForKey("Five")))->getCString();
  70. break;
  71. case 7:
  72. rank_str = ((__String*)(dictionary->objectForKey("Four")))->getCString();
  73. break;
  74. case 8:
  75. rank_str = ((__String*)(dictionary->objectForKey("Three")))->getCString();
  76. break;
  77. case 9:
  78. rank_str = ((__String*)(dictionary->objectForKey("Two")))->getCString();
  79. break;
  80. case 10:
  81. rank_str = ((__String*)(dictionary->objectForKey("One")))->getCString();
  82. break;
  83. default:
  84. rank_str = ((__String*)(dictionary->objectForKey("Zere")))->getCString();
  85. break;
  86. };
  87.  
  88. auto m_label1 = Label::createWithTTF(
  89. rank_str.getCString(),65
  90. );
  91. m_label1->setColor(Color3B(255,0));
  92. m_label1->setPosition(Point(visibleSize.width / 2,visibleSize.height / 2 - m_label1->getContentSize().height));
  93.  
  94. this->addChild(m_label1);
  95.  
  96.  
  97. /////////////////////////////
  98. // 2. add a menu item with "X" image,which is clicked to quit the program
  99. // you may modify it.
  100.  
  101. //退出游戏 按钮
  102. auto tempClose1 = Sprite::createWithSpriteFrameName("GameOver_nor.png");
  103. auto tempClose2 = Sprite::createWithSpriteFrameName("GameOver_touched.png");
  104.  
  105. auto closeItem = MenuItemSprite::create(
  106. tempClose1,tempClose2,CC_CALLBACK_1(GameOver::menuCloseCallback,this)
  107. );
  108.  
  109. //返回主界面 按钮
  110. auto tempBack1 = Sprite::createWithSpriteFrameName("ReturnGame_nor.png");
  111. auto tempBack2 = Sprite::createWithSpriteFrameName("ReturnGame_touched.png");
  112.  
  113. auto backItem = MenuItemSprite::create(
  114. tempBack1,tempBack2,CC_CALLBACK_1(GameOver::menuMainCallback,this)
  115. );
  116.  
  117. // create menu,it's an autorelease object
  118. auto menu = Menu::create(closeItem,backItem,NULL);
  119. menu->alignItemsVerticallyWithPadding(closeItem->getContentSize().height / 2);
  120. menu->setPosition(Vec2(origin.x + visibleSize.width / 2,visibleSize.height / 4));
  121. CC_BREAK_IF(!menu);
  122.  
  123. this->addChild(menu,1);
  124.  
  125. bRect = true;
  126. } while (0);
  127.  
  128. /////////////////////////////
  129. // 3. add your codes below...
  130.  
  131.  
  132. return true;
  133. }
  134.  
  135. // 退出游戏
  136. void GameOver::menuCloseCallback(Ref* pSender)
  137. {
  138. Director::getInstance()->end();
  139.  
  140. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
  141. exit(0);
  142. #endif
  143. }
  144.  
  145. // 返回主界面
  146. void GameOver::menuMainCallback(cocos2d::Ref* pSender)
  147. {
  148. CocosDenshion::SimpleAudioEngine::getInstance()->stopBackgroundMusic(true);
  149. Director::getInstance()->replaceScene(TransitionProgressRadialCCW::create(0.8f,HelloWorld::createScene()));
  150. }

猜你在找的Cocos2d-x相关文章