当前位置: 首页 > 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"); // 跳转到结束场景

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

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

相关文章:

  • 经验分享:大数据多头借贷风险对自身的不利影响?
  • OpenCV 图像处理 轮廓检测基本原理
  • C 语言动态顺序表
  • 擅于辩论的人可以将黑的说成白的,但是存在无法解决的矛盾
  • java的命令执行漏洞揭秘
  • 爬虫中常见的加密算法Base64伪加密,MD5加密【DES/AES/RSA/SHA/HMAC】及其代码实现(一)
  • C语言数据在内存中的存储超详解
  • 【大模型】【NL2SQL】基本原理
  • RK3568平台(显示篇)DRM vop驱动程序分析
  • vue3 动态加载组件
  • Latex on overleaf入门语法
  • 使用Echarts来实现数据可视化
  • 一文搞懂GIT
  • jQuery入门(四)案例
  • 揭秘MITM攻击:原理、手法与防范措施
  • 【YOLOv8】一文全解+亮点介绍+训练教程+独家魔改优化技巧
  • 创建mvp ubo(uniform buffer object)
  • 1.GPIO
  • C++必修:STL之vector的了解与使用
  • 【MySQL】索引 【上】 {没有索引的查询/磁盘/mysql与磁盘IO/初识索引}
  • GO goroutine状态流转
  • DLMS/COSEM中的信息安全:DLMS/COSEM安全概念(上)
  • C语言第九天笔记
  • 智慧环卫可视化:科技赋能城市清洁管理
  • 【力扣】SQL题库练习5
  • 永结无间Ⅸ--你不需要LLM Agent
  • Simulink|基于粒子群算法的永磁同步电机多参数辨识
  • 程序如何自动点击亚马逊商户后台的“邀请评论”按钮
  • 大模型算法面试题(十八)
  • 手机在网状态接口如何对接?(二)