Cocos Creator2D游戏开发(10)-飞机大战(8)-计分和结束
创始人
2024-11-16 04:33:38
0

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

  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"); // 跳转到结束场景 

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

相关内容

热门资讯

一分钟内幕!科乐吉林麻将系统发... 一分钟内幕!科乐吉林麻将系统发牌规律,福建大玩家确实真的是有挂,技巧教程(有挂ai代打);所有人都在...
一分钟揭秘!微扑克辅助软件(透... 一分钟揭秘!微扑克辅助软件(透视辅助)确实是有挂(2024已更新)(哔哩哔哩);1、用户打开应用后不...
五分钟发现!广东雀神麻雀怎么赢... 五分钟发现!广东雀神麻雀怎么赢,朋朋棋牌都是是真的有挂,高科技教程(有挂方法)1、广东雀神麻雀怎么赢...
每日必看!人皇大厅吗(透明挂)... 每日必看!人皇大厅吗(透明挂)好像存在有挂(2026已更新)(哔哩哔哩);人皇大厅吗辅助器中分为三种...
重大科普!新华棋牌有挂吗(透视... 重大科普!新华棋牌有挂吗(透视)一直是有挂(2021已更新)(哔哩哔哩)1、完成新华棋牌有挂吗的残局...
二分钟内幕!微信小程序途游辅助... 二分钟内幕!微信小程序途游辅助器,掌中乐游戏中心其实存在有挂,微扑克教程(有挂规律)二分钟内幕!微信...
科技揭秘!jj斗地主系统控牌吗... 科技揭秘!jj斗地主系统控牌吗(透视)本来真的是有挂(2025已更新)(哔哩哔哩)1、科技揭秘!jj...
1分钟普及!哈灵麻将攻略小,微... 1分钟普及!哈灵麻将攻略小,微信小程序十三张好像存在有挂,规律教程(有挂技巧)哈灵麻将攻略小是一种具...
9分钟教程!科乐麻将有挂吗,传... 9分钟教程!科乐麻将有挂吗,传送屋高防版辅助(总是存在有挂)1、完成传送屋高防版辅助透视辅助安装,帮...
每日必看教程!兴动游戏辅助器下... 每日必看教程!兴动游戏辅助器下载(辅助)真是真的有挂(2025已更新)(哔哩哔哩)1、打开软件启动之...