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

时间:2022-11-04 15:32:43

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

	// 退出游戏
void menuCloseCallback(cocos2d::Ref* pSender); // 返回主界面
void menuMainCallback(cocos2d::Ref* pSender);

实现该功能的代码例如以下

bool GameOver::init()
{
//////////////////////////////
// 1. super init first
if (!Layer::init())
{
return false;
} bool bRect = false; //背景音乐
if (CocosDenshion::SimpleAudioEngine::getInstance()->isBackgroundMusicPlaying())
{
CocosDenshion::SimpleAudioEngine::getInstance()->stopBackgroundMusic(true);
CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic("sound/game_over.mp3", true);
} do
{
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin(); //加入背景图片
auto m_background = Sprite::createWithSpriteFrameName("backgroundGameOver.png");
m_background->setPosition(Point(visibleSize.width / 2, visibleSize.height / 2));
m_background->setAnchorPoint(Vec2(0.5, 0.5));
CC_BREAK_IF(!m_background); this->addChild(m_background); //加入分数
auto score_int = UserDefault::getInstance()->getIntegerForKey("currentScore");
auto score_str = __String::createWithFormat("%d", score_int);
auto score = Label::createWithTTF(score_str->getCString(), "fonts/DFPShaoNvW5-GB.ttf", 40);
score->setPosition(Point(visibleSize.width / 2, visibleSize.height/3*2));
score->setColor(Color3B(255, 0, 0));
CC_BREAK_IF(!score); this->addChild(score); //设定等级 //设置标签 并 获取中文文本
auto dictionary = Dictionary::createWithContentsOfFile("fonts/AboutMe.xml");
String rank_str; switch (score_int/1000)
{
case 0:
rank_str = ((__String*)(dictionary->objectForKey("Eleven")))->getCString();
break;
case 1:
rank_str = ((__String*)(dictionary->objectForKey("Ten")))->getCString();
break;
case 2:
rank_str = ((__String*)(dictionary->objectForKey("Nine")))->getCString();
break;
case 3:
rank_str = ((__String*)(dictionary->objectForKey("Eight")))->getCString();
break;
case 4:
rank_str = ((__String*)(dictionary->objectForKey("Seven")))->getCString();
break;
case 5:
rank_str = ((__String*)(dictionary->objectForKey("Six")))->getCString();
break;
case 6:
rank_str = ((__String*)(dictionary->objectForKey("Five")))->getCString();
break;
case 7:
rank_str = ((__String*)(dictionary->objectForKey("Four")))->getCString();
break;
case 8:
rank_str = ((__String*)(dictionary->objectForKey("Three")))->getCString();
break;
case 9:
rank_str = ((__String*)(dictionary->objectForKey("Two")))->getCString();
break;
case 10:
rank_str = ((__String*)(dictionary->objectForKey("One")))->getCString();
break;
default:
rank_str = ((__String*)(dictionary->objectForKey("Zere")))->getCString();
break;
}; auto m_label1 = Label::createWithTTF(
rank_str.getCString(),
"fonts/DFPShaoNvW5-GB.ttf",
65
);
m_label1->setColor(Color3B(255, 0, 0));
m_label1->setPosition(Point(visibleSize.width / 2, visibleSize.height / 2 - m_label1->getContentSize().height)); this->addChild(m_label1); /////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it. //退出游戏 button
auto tempClose1 = Sprite::createWithSpriteFrameName("GameOver_nor.png");
auto tempClose2 = Sprite::createWithSpriteFrameName("GameOver_touched.png"); auto closeItem = MenuItemSprite::create(
tempClose1, tempClose2, CC_CALLBACK_1(GameOver::menuCloseCallback, this)
); //返回主界面 button
auto tempBack1 = Sprite::createWithSpriteFrameName("ReturnGame_nor.png");
auto tempBack2 = Sprite::createWithSpriteFrameName("ReturnGame_touched.png"); auto backItem = MenuItemSprite::create(
tempBack1, tempBack2, CC_CALLBACK_1(GameOver::menuMainCallback, this)
); // create menu, it's an autorelease object
auto menu = Menu::create(closeItem, backItem, NULL);
menu->alignItemsVerticallyWithPadding(closeItem->getContentSize().height / 2);
menu->setPosition(Vec2(origin.x + visibleSize.width / 2, visibleSize.height / 4));
CC_BREAK_IF(!menu); this->addChild(menu, 1); bRect = true;
} while (0); /////////////////////////////
// 3. add your codes below... return true;
} // 退出游戏
void GameOver::menuCloseCallback(Ref* pSender)
{
Director::getInstance()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
} // 返回主界面
void GameOver::menuMainCallback(cocos2d::Ref* pSender)
{
CocosDenshion::SimpleAudioEngine::getInstance()->stopBackgroundMusic(true);
Director::getInstance()->replaceScene(TransitionProgressRadialCCW::create(0.8f,HelloWorld::createScene()));
}