微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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


	// 退出游戏
	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));
		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(),65
			);
		m_label1->setColor(Color3B(255,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.

		//退出游戏 按钮
		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)
			);

		//返回主界面 按钮
		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()));
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐