【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, 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()));
}
【cocos2d-x 3.7 飞机大战】 决战南海I (十二) 游戏结束场景的更多相关文章
- 【cocos2d-x 3.7 飞机大战】 决战南海I (十) 游戏主场景
主场景要包括其它类的头文件 #include "cocos2d.h" #include "MyPlane.h" #include "Bullet.h& ...
- javascript飞机大战-----001分析
1.游戏引擎 首先要做飞机大战要考虑的是这个游戏被分成了哪几大部分?这样我们一块一块去做,特别清晰明了.那么接下来我们就简单的分析下飞机大战分成了哪几大部分 1.游戏引擎 2.英雄机 3.敌机 4.子 ...
- 11.pygame飞机大战游戏整体代码
主程序 # -*- coding: utf-8 -*- # @Time: 2022/5/20 22:26 # @Author: LiQi # @Describe: 主程序 import pygame ...
- cocos2dx实现经典飞机大战
游戏开始层 #ifndef __LayerGameStart_H__ #define __LayerGameStart_H__ #include "cocos2d.h" USING ...
- 【一】仿微信飞机大战cocos2d-x3.0rc1
參考 [偶尔e网事] 的 [cocos2d-x入门实战]微信飞机大战 cocos2dx 2.0版本号,偶尔e网事他写的很具体,面面俱到,大家很有必要看下.能够通过以下链接跳转: cocos2d-x入 ...
- 微信5.0 Android版飞机大战破解无敌模式手记
微信5.0 Android版飞机大战破解无敌模式手记 转载: http://www.blogjava.net/zh-weir/archive/2013/08/14/402821.html 微信5.0 ...
- 基于Cocos2d-x-1.0.1的飞机大战游戏开发实例(上)
最近接触过几个版本的cocos2dx,决定每个大变动的版本都尝试一下.本实例模仿微信5.0版本中的飞机大战游戏,如图: 一.工具 1.素材:飞机大战的素材(图片.声音等)来自于网络 2.引擎:coco ...
- Cocos2d-x飞机大战教程笔记
咳咳~跟着大神的教程学做Cocos2d-x的飞机大战...鉴于我是那种跟着教程都会出非常多错的人,所以还是一路跟着做些笔记比較好.并且因为是用课余时间,所以仅仅能断断续续地做,写下来也好让自己别忘记~ ...
- js实例--飞机大战
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title> ...
随机推荐
- ACM_小明滚出去?(求逆序数)
小明滚出去? Time Limit: 2000/1000ms (Java/Others) Problem Description: 老师:“小明,写一个排序算法”: 小明: void mysort(i ...
- 前端HTML中float学习笔记
float元素原本的作用是用来使文字包裹图片,现在人们更多的是用来进行布局(ps:有没有点滥用的意思) 也就是说本来你排好的界面设计,但是因为浮动会导致元素脱离文档流,使得其他非浮动的块级元素会无视这 ...
- angualr2之路由与导航(一)
1. 唯一根模块式组件导航 在唯一根模块式组件导航中,全局只有一个模块,即根模块. 1.1 根模块 在根模块中,需要导入所有组件及其对应的服务,并将组件加入声明,将其对应的服务加入提供商. // ap ...
- AI不与人为敌
业界广为流传着一句话:有多少人工就有多少智能. 其实还应该有一句话:有多少付出就因该有多少回报.公正是世界永恒的话题. 一.人工智能还是人工愚蠢 科技从来没有善恶,也不会杀人,愚蠢的人比聪明的人做的错 ...
- numpy安装失败-小失误
1. 古老的方法: 安装python numpy库AMD64 失败,网上的教程是这样的:http://www.cnblogs.com/zhuyp1015/archive/2012 ...
- window 8 电脑操作服务集合(网址)
如何开启Win8远程桌面 http://jingyan.baidu.com/album/48206aeae06627216ad6b3bf.html?picindex=2 Win8.1用户账户的配置管理 ...
- spirngMvc
配置方式就略了 直接开始注解方式: 1. 新建项目 2. 导入jar包 3. 创建controller,用注解方式声明 4. 在web.xml配置核心分发器DispatcherServlet ...
- PHP并发IO编程实践
PHP相关扩展 Stream:PHP内核提供的socket封装 Sockets:对底层Socket API的封装 Libevent:对libevent库的封装 Event:基于Libevent更高级的 ...
- MySQL基础命令小结
数据库授权登录[root@localhost ~]# mysql -uroot -p123456mysql> grant select,lock tables on auth.* to 'adm ...
- jsp+servlet 导出Excel表格
1.项目的目录结构 2.创建一个用户类,下面会通过查询数据库把数据封装成用户实例列表 package csh.entity; /** * @author 悦文 * @create 2018-10-24 ...