Cocos Creator2D游戏开发(10)-飞机大战(8)-计分和结束
创始人
2024-11-16 03:34:20
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"); // 跳转到结束场景 

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

相关内容

热门资讯

玩家攻略,金花房卡专卖店九酷众... 微信游戏中心:九酷众娱房卡在哪里买打开微信,添加客服微信【88355042】,进入游戏中心或相关小程...
正规平台有哪些,金花房卡批发价... 正规平台有哪些,金花房卡批发价火神大厅/房卡链接怎么获取Sa9Ix苹果iPhone 17手机即将进入...
ia攻略/金花房卡如何购买嫦娥... 嫦娥大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:【3329006910】或QQ:332900...
推荐一款!牛牛充值房卡海草众厅... 海草众厅房卡更多详情添加微:33549083、 2、在商城页面中选择房卡选项。 3、根...
一分钟了解!金花房卡出售红桃众... 一分钟了解!金花房卡出售红桃众娱/微信链接房卡充值链接红桃众娱是一款非常受欢迎的游戏,咨询房/卡添加...
我来教你/金花房卡制作链接熊猫... 我来教你/金花房卡制作链接熊猫大厅/微信链接房卡批发价Sa9Ix苹果iPhone 17手机即将进入量...
科技实测!金花房卡出售新八戒/... 新八戒房卡更多详情添加微:33549083、 2、在商城页面中选择房卡选项。 3、根据...
ia攻略/斗牛房卡充值乐乐大厅... 微信游戏中心:乐乐大厅房卡在哪里买打开微信,添加客服微信【88355042】,进入游戏中心或相关小程...
ia攻略/金花房卡批发嫦娥大厅... 嫦娥大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:【3329006910】或QQ:332900...
头条推荐!金花房卡出售乐乐大厅... 头条推荐!金花房卡出售乐乐大厅//全网房卡低价售Sa9Ix苹果iPhone 17手机即将进入量产阶段...
重大通报,金花房卡制作链接黄帝... 您好!微信黄帝大厅大厅链接获取房卡可以通过以下几种方式购买: 1.微信渠道:(黄帝大厅)大厅介绍:...
头条推荐!金花房卡是正规的兄弟... 兄弟大厅/新道游房卡更多详情添加微:33549083、 2、在商城页面中选择房卡选项。 ...
科技实测!怎么买斗牛房卡龙马大... 龙马大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:【3329006910】或QQ:332900...
科技实测!游戏推荐斗牛房卡出售... 您好!微信悠悠众娱大厅链接获取房卡可以通过以下几种方式购买: 1.微信渠道:(悠悠众娱)大厅介绍:...
玩家攻略,怎么买斗牛房卡至尊大... 今 日消息,至尊大厅房卡添加微信33549083 苹果今日发布了 iOS 16.1 正式版更新,简单...
玩家攻略,游戏推荐牛牛房卡出售... 金牛座厅/新西部房卡更多详情添加微:33549083、 2、在商城页面中选择房卡选项。 ...
IA解析/怎么买斗牛房卡海蓝大... IA解析/怎么买斗牛房卡海蓝大厅/开群怎么买房卡海蓝大厅是一款非常受欢迎的游戏,咨询房/卡添加微信:...
我来教你/金花房卡批发价龙王大... 我来教你/金花房卡批发价龙王大厅/房卡怎么搞Sa9Ix苹果iPhone 17手机即将进入量产阶段。有...
我来教你/牛牛房卡制作链接卡农... 今 日消息,卡农大厅房卡添加微信33549083 苹果今日发布了 iOS 16.1 正式版更新,简单...
IA解析/金花房卡专卖店芝麻大... 微信游戏中心:芝麻大厅房卡在哪里买打开微信,添加客服微信【88355042】,进入游戏中心或相关小程...