【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> ...
随机推荐
- WebService常用技术及术语
一.Web Service是什么? 1. 基于Web的服务:服务器端整出一些资源让客户端应用访问(获取数据) 2. 一个跨语言.跨平台的规范(抽象) 3. 多个跨平台.跨语言的应用间通信整合的方案(实 ...
- 使用logback实现http请求日志导入mongodb
spring boot自带logback作为其日志新系统,但是在实际工作中,常常需要对日志进行管理或分析,如果只是单纯的将日志导入文本文件,则在查询时操作过于繁琐,如果将其导入mysql等关系型数据库 ...
- Windows7下caffe-ssd-microsoft下编译
整个编译可谓漫长 编译了两天 网上教程也很多 但是也很杂 遇到各种错误 总归是编完了 1.下载Windows版本的Caffe-SSD源码 下载链接:https://github.com/conner9 ...
- Ubuntu1804安装Postgresql【转】
转载自:https://huur.cn/course/yw/1591.html 关系数据库管理系统是许多网站和应用程序的关键组成部分.它们提供了一种结构化的方式来存储,组织和访问信息. Postgre ...
- python练习--1、简易登录接口
function:用于实现简单的用户登录接口. description:用户信息存储于字典中,增加用户需手动写入代码中.最多可以尝试登录9次,首次使用会先在程序目录下创建锁文件.若在用户栏输入quit ...
- 面试回答问题要防范hr的陷阱
面试技巧是大众化的,比较实在.在经历的各种面试中,最不容易防范的面试就是“拉呱”(山东方言,聊天的意思),这样子自己容易放松警惕. 看看下面的,就知道应该怎么应对这些“滑头”的HR! 1.我们为什么要 ...
- (转)基于Metronic的Bootstrap开发框架经验总结(2)--列表分页处理和插件JSTree的使用
http://www.cnblogs.com/wuhuacong/p/4759564.html 在上篇<基于Metronic的Bootstrap开发框架经验总结(1)-框架总览及菜单模块的处理& ...
- [Jxoi2012]奇怪的道路 题解(From luoguBlog)
题面 状压好题 1<= n <= 30, 0 <= m <= 30, 1 <= K <= 8 这美妙的范围非状压莫属 理所当然地,0和1代表度的奇偶 dp[i][j ...
- Git更新代码
此次更新,创建新的文件与文件夹,并且删除了原有文件 通过“git status” 命令查看当前变更.通过变更信息可以看出,删除了test_case.py文件.这个删除只是在项目目录下进行删除,Git对 ...
- ionic Plugin插件,与原生app端交互,ionic端代码
创建plugins 目录 definitions.ts文件 definitions.ts文件: import {Plugin} from '@capacitor/core/dist/esm/defin ...