博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【cocos2d-x 3.7 飞机大战】 决战南海I (十二) 游戏结束场景
阅读量:7261 次
发布时间:2019-06-29

本文共 4373 字,大约阅读时间需要 14 分钟。

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

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

转载地址:http://dildm.baihongyu.com/

你可能感兴趣的文章
(转)Excel VBA 操作 Word(入门篇)
查看>>
.NET(C#)开源代码分析
查看>>
Android Notes
查看>>
调整数组顺序使奇数位于偶数前面
查看>>
从零开始--系统深入学习android(实践-让我们开始写代码-新手指南-6.使用Fragments构建动态UI)...
查看>>
opencv开发的程序分发给客户时所需要的dll文件
查看>>
MakeGenericMethod Reflection 反射 ObjectToXml<T> Generic XmlSerializer
查看>>
关于验收测试的几个困惑
查看>>
MVC项目引用备注
查看>>
vue2+vuex+vue-router 快速入门(五) vuex 介绍
查看>>
Nginx查看并发连接数
查看>>
awk next以及getline用法示例
查看>>
spring注解
查看>>
电信计费软件故障处理的六种模式
查看>>
类的执行过程
查看>>
python标记
查看>>
ASP.NET 学习笔记_06 Httphandler
查看>>
.NET后台访问其他站点代码整理
查看>>
Vuex 通俗版教程告诉你Vuex怎么用
查看>>
一些说明
查看>>