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

Cocos Creator2D游戏开发(10)-飞机大战(8)-计分和结束

现在游戏基本能完了, 飞机能发射子弹,打了敌机,敌机也能炸;
接下来要做计分了;
步骤:

  1. 搞出一个lable
  2. 让lable显示炸了多少飞机
    开搞:
    ①创建一个Lable标签
    在这里插入图片描述
    ② root.ts文件
    添加
    @property(Label) player_score: Label; // 标签属性
    标签绑定
    在这里插入图片描述
    ③ 代码添加
    注册 然后回调
    contactListener() {// 注册全局碰撞回调函数if (PhysicsSystem2D.instance) {PhysicsSystem2D.instance.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);}}// 全局的碰撞检测onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {// 只在两个碰撞体开始接触时被调用一次//console.log('Main_ROOT -> onBeginContact');console.log('selfCollider : ' + selfCollider.name);console.log('otherCollider : ' + otherCollider.name);if (selfCollider.name.includes('enemy_prefab') && otherCollider.name.includes('playerBullet_prefab')) {this.score++;this.player_score.string = "当前得分: " + this.score;}if (selfCollider.name.includes('player_node') && otherCollider.name.includes('enemy_prefab')) {this.player_score.string = '被炸了';}

root.ts完整代码

import { _decorator, Collider2D, Component, Contact2DType, instantiate, IPhysics2DContact, Label, Node, PhysicsSystem2D, Prefab } from 'cc';
const { ccclass, property } = _decorator;@ccclass('root')
export class root extends Component {@property(Node) enemy_node: Node;   // 添加属性 以便在代码中引用 敌人节点@property(Prefab) enemy_prefab: Prefab;   // 添加属性 以便在代码中 动态生成 敌人@property(Label) player_score: Label;   // 添加属性 以便在代码中 动态生成 敌人private score = 0;start() {this.createEnemies(); //使用定时器生成敌机this.contactListener();}update(deltaTime: number) {}contactListener() {// 注册全局碰撞回调函数if (PhysicsSystem2D.instance) {PhysicsSystem2D.instance.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);}}// 全局的碰撞检测onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {// 只在两个碰撞体开始接触时被调用一次//console.log('Main_ROOT -> onBeginContact');console.log('selfCollider : ' + selfCollider.name);console.log('otherCollider : ' + otherCollider.name);if (selfCollider.name.includes('enemy_prefab') && otherCollider.name.includes('playerBullet_prefab')) {this.score++;this.player_score.string = "当前得分: " + this.score;}if (selfCollider.name.includes('player_node') && otherCollider.name.includes('enemy_prefab')) {this.player_score.string = '被炸了';}
}createEnemies() {this.schedule(() => {this.createOneEnemy();}, 1);}// 创建一个敌机createOneEnemy() {// console.log("createOneGold !");const enemyPrefab = instantiate(this.enemy_prefab);   // 实例化一个对象 (动态生成)this.enemy_node.addChild(enemyPrefab);     // 将对象 添加到某个 节点内let x = (Math.random() * 360)   // random() 0-360 enemyPrefab.setPosition(180 - x, 350);  // 设置敌机的Y坐标}
}

剩下的就是结束场景
创建一个gameOver_scene
一个GameOver_node
一个retry_button
还有俩脚本retry_button.ts
GameOver.ts
绑定
在这里插入图片描述

在这里插入图片描述
最重要的是这里
在这里插入图片描述
retry_button.ts脚本文件

import { _decorator, Component, director, Node } from 'cc';
const { ccclass, property } = _decorator;@ccclass('ButtonRetry')
export class ButtonRetry extends Component {start() {}update(deltaTime: number) {}// 跳转到主场景gotoMainScene() {director.loadScene("main_scene");}
}

GameOver .ts

import { _decorator, Component, director, Label, Node } from 'cc';const { ccclass, property } = _decorator;@ccclass('GameOver')
export class GameOver extends Component {@property(Label) ScoreLabel: Label;   // 添加属性 以便在代码中 修改得分start() {this.ScoreLabel.string = "游戏结束";}update(deltaTime: number) {}
}

root.ts脚本中需要添加

  director.loadScene("gameOver_scene"); // 跳转到结束场景

这一章写的有点糙, 先这样吧, 这些天有点累了
后面的章节是小游戏部署到微信小游戏平台;
和登陆获取信息和跟后台交互

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • Oracle的RAC集群安装和配置 NFS 共享存储(NAS共享存储)
  • pytest之fixture
  • 过期知识:thinkphp5 使用migrate给现有的数据表新增表字段
  • 爬虫数据模拟真实设备请求头User-Agent生成(fake_useragent:一个超强的Python库)
  • 【第四章】测试理论与方法 - 黑盒测试
  • 最全面的Python重点知识汇总,建议收藏!
  • 销量激增难解奇瑞焦虑:新能源短板与加班文化引争议
  • 【PyTorch】安装pytorch方法总结
  • 【C++】C++应用案例-通讯录管理系统
  • 守护数据堡垒:SQL Server数据库自定义备份审计实现指南
  • [Meachines] [Easy] Sense PFSense防火墙RCE
  • Golang | Leetcode Golang题解之第318题最大单词长度乘积
  • python常用库
  • 【selenium:webdriver原理】
  • 达梦数据库的系统视图v$cachesql
  • Apache的基本使用
  • java取消线程实例
  • Lucene解析 - 基本概念
  • MQ框架的比较
  • nginx 负载服务器优化
  • ReactNativeweexDeviceOne对比
  • Spring技术内幕笔记(2):Spring MVC 与 Web
  • 百度地图API标注+时间轴组件
  • 干货 | 以太坊Mist负责人教你建立无服务器应用
  • 机器学习学习笔记一
  • 那些被忽略的 JavaScript 数组方法细节
  • 前端设计模式
  • 实战|智能家居行业移动应用性能分析
  • 使用putty远程连接linux
  • 详解NodeJs流之一
  • 在Mac OS X上安装 Ruby运行环境
  • ​Spring Boot 分片上传文件
  • #我与Java虚拟机的故事#连载15:完整阅读的第一本技术书籍
  • (02)Unity使用在线AI大模型(调用Python)
  • (06)金属布线——为半导体注入生命的连接
  • (done) NLP “bag-of-words“ 方法 (带有二元分类和多元分类两个例子)词袋模型、BoW
  • (STM32笔记)九、RCC时钟树与时钟 第二部分
  • (多级缓存)缓存同步
  • (含react-draggable库以及相关BUG如何解决)固定在左上方某盒子内(如按钮)添加可拖动功能,使用react hook语法实现
  • (蓝桥杯每日一题)平方末尾及补充(常用的字符串函数功能)
  • (六)Hibernate的二级缓存
  • (四) Graphivz 颜色选择
  • (转)【Hibernate总结系列】使用举例
  • (转)Android中使用ormlite实现持久化(一)--HelloOrmLite
  • (转载)深入super,看Python如何解决钻石继承难题
  • * CIL library *(* CIL module *) : error LNK2005: _DllMain@12 already defined in mfcs120u.lib(dllmodu
  • .net MVC中使用angularJs刷新页面数据列表
  • .net 验证控件和javaScript的冲突问题
  • .NET国产化改造探索(三)、银河麒麟安装.NET 8环境
  • .NET中两种OCR方式对比
  • .php结尾的域名,【php】php正则截取url中域名后的内容
  • @Autowired自动装配
  • @hook扩展分析
  • [ 第一章] JavaScript 简史
  • [acm算法学习] 后缀数组SA