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

	// 退出游戏
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 (十二) 游戏结束场景的更多相关文章

  1. 【cocos2d-x 3.7 飞机大战】 决战南海I (十) 游戏主场景

    主场景要包括其它类的头文件 #include "cocos2d.h" #include "MyPlane.h" #include "Bullet.h& ...

  2. javascript飞机大战-----001分析

    1.游戏引擎 首先要做飞机大战要考虑的是这个游戏被分成了哪几大部分?这样我们一块一块去做,特别清晰明了.那么接下来我们就简单的分析下飞机大战分成了哪几大部分 1.游戏引擎 2.英雄机 3.敌机 4.子 ...

  3. 11.pygame飞机大战游戏整体代码

    主程序 # -*- coding: utf-8 -*- # @Time: 2022/5/20 22:26 # @Author: LiQi # @Describe: 主程序 import pygame ...

  4. cocos2dx实现经典飞机大战

    游戏开始层 #ifndef __LayerGameStart_H__ #define __LayerGameStart_H__ #include "cocos2d.h" USING ...

  5. 【一】仿微信飞机大战cocos2d-x3.0rc1

    參考 [偶尔e网事] 的 [cocos2d-x入门实战]微信飞机大战  cocos2dx 2.0版本号,偶尔e网事他写的很具体,面面俱到,大家很有必要看下.能够通过以下链接跳转: cocos2d-x入 ...

  6. 微信5.0 Android版飞机大战破解无敌模式手记

    微信5.0 Android版飞机大战破解无敌模式手记 转载: http://www.blogjava.net/zh-weir/archive/2013/08/14/402821.html 微信5.0 ...

  7. 基于Cocos2d-x-1.0.1的飞机大战游戏开发实例(上)

    最近接触过几个版本的cocos2dx,决定每个大变动的版本都尝试一下.本实例模仿微信5.0版本中的飞机大战游戏,如图: 一.工具 1.素材:飞机大战的素材(图片.声音等)来自于网络 2.引擎:coco ...

  8. Cocos2d-x飞机大战教程笔记

    咳咳~跟着大神的教程学做Cocos2d-x的飞机大战...鉴于我是那种跟着教程都会出非常多错的人,所以还是一路跟着做些笔记比較好.并且因为是用课余时间,所以仅仅能断断续续地做,写下来也好让自己别忘记~ ...

  9. js实例--飞机大战

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title> ...

随机推荐

  1. ACM_名字的价值

    名字的价值 Time Limit: 2000/1000ms (Java/Others) Problem Description: 集训终于开始了,参加集训的人很多,也就有很多名字,集训组织者发现了一件 ...

  2. sql 添加列并设置默认值

    ALTER TABLE tablsename ADD fieldname BIT NULL DEFAULT

  3. css2.0文档查阅及字体样式

    css2.0文档查阅下载     网址:http://soft.hao123.com/soft/appid/9517.html <html xmlns="http://www.w3.o ...

  4. Java基础12一IO流

    1.IO流的原理 利用数据通道实现程序和数据源之间数据的的读写操作.  2.IO流分类 输入流.输出流.字节流.字符流.节点流.过滤流  3.InputStream 字节输入流 实现类FileInpu ...

  5. c++ 中一个类或者一个对象所占的字节数

    转载博客:转载地址https://www.cnblogs.com/JingHuanXiao/p/6080726.html 一个空的class在内存中多少字节?如果加入一个成员函数后是多大?这个成员函数 ...

  6. C语言笔记(一)

    笑话一枚:程序员 A:“哥们儿,最近手头紧,借点钱?”程序员 B:“成啊,要多少?”程序员 A:“一千行不?”程序员 B:“咱俩谁跟谁!给你凑个整,1024,拿去吧.” =============== ...

  7. 查看SqlServer连接所使用的端口号

    最近一个项目里用到了一个插件,在配置时发现连接数据库使用的是JDBC,输入URL时用到了端口号.印象中在使用Sqlserver时貌似没有提到端口号,在网上查阅了一下,记录下来省的忘了 方法是通过内置的 ...

  8. 说说web缓存-强缓存、协商缓存

    网上关于WEB缓存的文章很多,今天汇总一下. 为什么要用缓存 一般针对静态资源如CSS,JS,图片等使用缓存,原因如下: 请求更快:通过将内容缓存在本地浏览器或距离最近的缓存服务器(如CDN),在不影 ...

  9. javaee字符文件的复制

    package Zy; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWrit ...

  10. Html-如何正确给table加边框

    一般来说,给表格加边框都会出现不同的问题,以下是给表格加边框后展现比较好的方式 <style> table,table tr th, table tr td { border:1px so ...