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

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

概述        游戏结束的时候,要显示分数,还要能够选择是返回主场景还是退出游戏 // 退出游戏 void menuCloseCallback(cocos2d::Ref* pSender); // 返回主界面 void menuMainCallback(cocos2d::Ref* pSender); 实现该功能的代码如下 bool GameOver::init(){ ////////

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


	// 退出游戏	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()));}
总结

以上是内存溢出为你收集整理的【cocos2d-x 3.7 飞机大战】 决战南海I (十二) 游戏结束场景全部内容,希望文章能够帮你解决【cocos2d-x 3.7 飞机大战】 决战南海I (十二) 游戏结束场景所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1069131.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存