当前位置: 首页 > news >正文

飞机打方块(四)游戏结束

一、游戏结束显示

1.新建节点

1.新建gameover节点

2.绑定canvas

3.新建gameover容器

 4.新建文本节点

2.游戏结束逻辑 

Barrier.ts

update(dt: number) {//将自身生命值取整let num = Math.floor(this.num);//在Label上显示this.num_lb.string = num.toString();//获取GameController脚本let gc = cc.find("Canvas").getComponent("GameController");//自身移动if (gc.is_barrier_move == true) {this.node.y -= dt * this.speed;}//获取canvas节点let canvas = cc.find("Canvas");//如果自身到了屏幕最下方if (this.node.y <= -(canvas.height / 2)) {//获取GameController脚本let gc = cc.find("Canvas").getComponent("GameController");//调用游戏结束函数gc.gameover()}}

 GameController.ts

 @property({ type: cc.Node, displayName: "游戏结束时显示的节点", tooltip: "游戏结束时显示的节点" })gameover_UI: cc.Node = null;//破纪录文字节点,如果复活将删除原有破纪录节点lb: cc.Node = null;//显示最终分数的文字ultimately_score_lb: cc.Label = null;onLoad() {cc.game.setFrameRate(90)//恢复游戏,避免游戏暂停导致无法继续cc.director.resume();//给Canvas绑定事件this.canvas.on(cc.Node.EventType.TOUCH_MOVE, this.onMove, this)//开启碰撞引擎let manager = cc.director.getCollisionManager();manager.enabled = true;//如果要调试if (this.is_debug == true) {// 是否绘制碰撞组件的形状,默认为不绘制manager.enabledDebugDraw = true;//是否绘制碰撞组件的包围盒,默认为不绘制manager.enabledDrawBoundingBox = true;}//创建障碍物this.create_barrier();this.cre_bar_f = (this.barrier_height / this.barrier_speed) + Math.random() * this.generation_interval;//生成显示最终得分的文字let score_lb = new cc.Node;score_lb.parent = this.gameover_UI.children[0];score_lb.addComponent(cc.Label)score_lb.getComponent(cc.Label).string = "最终得分:" + Math.floor(this.score);score_lb.getComponent(cc.Label).overflow = cc.Label.Overflow.SHRINK;this.ultimately_score_lb = score_lb.getComponent(cc.Label);score_lb.color = cc.color(0, 0, 0, 255);}//游戏结束函数gameover() {//打印logcc.log("游戏结束");//将游戏暂停cc.director.pause();//显示游戏结束的UIthis.gameover_UI.active = true;//隐藏飞机this.plane.active = false;//更新最终分数this.ultimately_score_lb.string = "最终得分:" + Math.floor(this.score);//获取历史最高分let score = cc.sys.localStorage.getItem("score");//如果破纪录了,更新历史最高分if (this.score > score) {cc.sys.localStorage.setItem("score", this.score);cc.log("破纪录了");//生成显示破纪录的文字let lb = new cc.Node;lb.getComponent(cc.Label);lb.getComponent(cc.Label).string = "破纪录了";lb.color = cc.color(0, 0, 0, 255);lb.parent = this.gameover_UI.children[0];//获取破纪录文字节点this.lb = lb;}//隐藏所有特殊按钮this.special_btn.active = false;}

2.复活按钮 

1.新建button按钮节点,绑定按钮事件 

GameController.ts

 @property({ type: cc.Node, displayName: "复活按钮", tooltip: "复活按钮,点击一次复活按钮将隐藏" })btn_resurgence: cc.Node = null//复活函数resurgence() {//恢复暂停的场景cc.director.resume();//关闭游戏结束时显示的UIthis.gameover_UI.active = false;//移除所有障碍物,如果不移除,游戏将再次结束this.remove_all_barrier();//显示飞机this.plane.active = true;//隐藏复活按钮this.btn_resurgence.active = false;//显示所有特殊按钮this.special_btn.active = true;//销毁破纪录文字节点this.lb.destroy()}

 2.绑定canvas

 

复活一次后

 

3.重玩按钮 

1.新建button按钮节点,绑定按钮事件 

GameController

//重新开始游戏函数reply(){cc.director.loadScene("Game")}

4.返回开始界面按钮 

1.新建button按钮节点,绑定按钮事件 

 

 

http://www.lryc.cn/news/134149.html

相关文章:

  • 保研之旅1:西北工业大学电子信息学院夏令营
  • [WMCTF 2023] crypto
  • 图像分割unet系列------TransUnet详解
  • ASCII码-shellcode的技巧
  • spring cloud 之 dubbo nacos整合
  • MySQL如何进行表之间的关联更新
  • Docker创建 LNMP 服务+Wordpress 网站平台
  • node没有自动安装npm时,如何手动安装 npm
  • C# 使用递归方法实现汉诺塔步数计算
  • 窗口函数大揭秘!轻松计算数据累计占比,玩转数据分析的绝佳利器
  • 健康检测智能睡眠床垫方案
  • 计网第三章(数据链路层)(五)
  • 嵌入式系统中常见内存的划分方法
  • 深入理解与实现:常见搜索算法的Java示例
  • PHP自己的框架实现操作成功失败跳转(完善篇四)
  • 【汇编语言】CS、IP寄存器
  • Nvidia Jetson 编解码开发(3)解决H265解码报错“PPS id out of range”
  • Angular中如何获取URL参数?
  • uniapp编写微信小程序和H5遇到的坑总结
  • 课程表-广度优先和图
  • 机器学习|决策树:数学原理及代码解析
  • 1.0的星火2.0必将燎原——图文声影PPT全测试
  • [MySQL]主从服务器布置
  • 图像处理算法大全(基于libyuv或IPP)----NV12转成I420,RGB24,ARGB集合
  • 机器人操作系统:ROS2 仿真入门
  • 面试题:线程池的底层工作原理
  • Excel/PowerPoint条形图改变顺序
  • 【操作系统】虚拟内存相关分段分页页面置换算法
  • Unrecognized Hadoop major version number: 3.0.0-cdh6.3.2
  • 机器学习分类,损失函数中为什么要用Log,机器学习的应用