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

【Cocos TypeScript 零基础 22.1】

<羊了个羊>目录6

  • 生成卡片
  • 代码
    • 背景代码
    • 卡片代码
    • 音频代码
    • 布局

个人觉得
搭框架式写法
优势
项目整体有型
不会遗漏
缺点
大型项目推进慢,且容易因为目标过大(考虑的多)导致迟迟无法推进
搭积木式写法
优势
小目标明确,且一点点推进有成就感
遇到难点,也能随时变换思路,不用考虑整体效果
缺点
整个项目因为考虑不周全,会导致命名不规范及逻辑错误
错误不太清楚发生在哪里,因为贯通后,错误一旦发生就不知道错误出在哪里
之前的飞机飞机大战就是这样,现在也是一样
需要重写梳理 名称 及 逻辑 及 重复函数

生成卡片

为什么搞了这么久才来发
因为遇到瓶颈了,重写的2次,大规模修改代码,数理逻辑,修复bug
整体逻辑已经没有异常才来写心得
预制体 生成核心
一实例化
二设置父节点
三设置坐标
卡片生成锚点
这里需要注意一点我这里是使用了3个锚点生成卡片
如要像原版一样集中散开,应该是1个锚点散开,分组任然是多个
卡片生成顺序
先生成的预制体会被遮挡,
所以 node 数组的先后顺序决定生成顺序
比如我的
数组.push 设置父节点就要用倒叙的方法

代码

这里提一下命名一定要预先想好规则
否则之后再来 找或改 都是乱七八糟的,且麻烦

背景代码

import {
    _decorator, Camera, Component, director, instantiate, Label, Node, Prefab, ProgressBar, randomRangeInt, Vec3 } from 'cc';
import {
    ts_music } from './ts_music';
import {
    ts_card } from './ts_card';
const {
    ccclass, property } = _decorator;

@ccclass('s2_bg')
export class s2_bg extends Component {
   
    static inthis : s2_bg
    static getthis() : s2_bg {
   return this.inthis}

    arr_card0 : Node[] = []   //  移出区数组
    arr_card1 : Node[] = []  //  区块一下的实际卡片
    arr_card2 : Node[] = []  //  区块二下的实际卡片
    arr_card3 : Node[] = []  //  区块三下的实际卡片
    arr_wait : Node[] = []  //  消除区数组
    arr_card : Node[] = []  //  所有需要生成的卡片

    game_lv_up : number = 1     //  游戏最高关卡
    game_lv_now : number = 1    //  游戏当前关卡
    game_block : number = 2     //  区块
    game_time_up : number = 0   //  游戏上限时间
    game_time_dt : number = 0   //  游戏过去时间
    game_card : number[] = []   //  卡片选取种类
    game_boo : boolean = true   //  游戏正常
    //game_bao : number = 0       //  卡片包 本来打算设计卡片包这个参数使得游戏更复杂

    @property(Camera) cam : Camera = null
    @property(Label) ui_game_lv : Label = null
    @property(ProgressBar) ui_game_time : ProgressBar = null
    @property(Node) ui_blo0 : Node = null
    @property(Node) ui_blo1 : Node = null
    @property(Node) ui_blo2 : Node = null
    @property(Node) ui_blo3 : Node = null
    @property(Node) ui_blo_wait : Node = null
    
    @property(Node) ui_end_wd : Node = null
    @property(Label) ui_end_txt1 : Label = null
    @property(Label) ui_end_txt2 : Label = null
    @property(Label) ui_end_but_lb : Label = null

    @property(Prefab) card0 : Prefab = null
    @property(Prefab) card1 : Prefab = null
    @property(Prefab) card2 : Prefab = null
    @property(Prefab) card3 : Prefab = null
    @property(Prefab) card4 : Prefab = null
    @property(Prefab) card5 : Prefab = null
    @property(Prefab) card6 : Prefab = null
    @property(Prefab) card7 : Prefab = null
    @property(Prefab) card8 : Prefab = null
    @property(Prefab) card9 : Prefab = null
    @property(Prefab) card10 : Prefab = null
    @property(Prefab) card11 : Prefab = null
    @property(Prefab) card12 : Prefab = null
    @property(Prefab) card13 : Prefab = null
    
    start() {
   
        //localStorage.setItem(`game_lv_now` , `1`)     //  关数太高降低关数时启用
        s2_bg.inthis = this
        ts_music.getthis().on_bgm2()
        this.on_start()
    }
    update(deltaTime: number) {
   
        this.on_time(deltaTime)
    }
    /**游戏开始 */
    on_start(){
   
        this.mode_game_start()
        this.mode_game_card()
        this.mode_game_block()
        this.mode_game_set()
        director.resume()
    }
    /**游戏结束 */
    on_end(){
   
        this.ui_end_wd.active = true
        if (this.arr_card.length > 0){
   
            this.ui_end_txt1.string = `遗憾失败`
            let txt2 = localStorage.getItem(`game_lv_up`)
            this.ui_end_txt2.string = `历史最高 ${
     txt2}`
            this.ui_end_but_lb.string = `重新开始`
            this.game_lv_now = 1
            localStorage.setItem(`game_lv_now` , `1`)
        }else {
   
            this.ui_end_txt1.string = `恭喜过关`
            this.game_lv_up = Number(localStorage.getItem(`game_lv_up`))

            if (this.game_lv_now > this.game_lv_up){
   
                this.ui_end_txt2.string = `新纪录 ${
     this.game_lv_now}`
                localStorage.setItem(`game_lv_up` , this.game_lv_now.toString())
            }else {
   
                let txt2 = localStorage.getItem(`game_lv_up`)
                this.ui_end_txt2.string = `历史最高 ${
     txt2}`
            }

            this.ui_end_but_lb.string = `下一关`
            this.game_lv_now += 1
            localStorage.setItem(`game_lv_now` , this.game_lv_now.toString())
        }
        this.game_boo = false
        console.log(`游戏结束`)
        director.pause()
    }
    /**游戏返回 */
    on_back(){
   
        ts_music.getthis().on_but()
        localStorage.setItem(`game_lv_now` , this.game_lv_now.toString())
        director.loadScene(`s1`)
    }
    /**卡片移动 */
    on_move(no : Node){
   
        if (this.arr_wait.length > 4){
   return}   //  消除区卡片上限5
        this.mode_click(no)
        this.mode_del(no)
        this.mode_set(no , -1)
        if (this.mode_find(no)){
       //  判断同名是否满足三次
            this.mode_des(no.name)
        }
        
        if (this.arr_card.length == 0){
   this.on_end()}
        else {
   console.log(`游戏继续 所有卡数量 ${
     this.arr_card.length}`)}
    }
    

http://www.kler.cn/a/594008.html

相关文章:

  • 云原生安全基座:零信任架构的原子化防御革命
  • 使用uniapp的vite版本进行微信小程序开发,在项目中使用mqtt连接、订阅、发布信息
  • C#入门学习记录(三)C#中的隐式和显示转换
  • html5-Canvas弹跳小球项目开发总结
  • Next.js 如何成功解决了React的致命缺陷?
  • ngx_http_conf_ctx_t
  • 【SpringCloud】微服务的治理以及服务间的远程调用
  • 网络工程学习笔记
  • idea问题(三)pom文件显示删除线
  • 深度学习之语义分割
  • 【算法day16】电话号码的字母组合
  • 【设计模式】策略模式
  • java使用Modbus协议与设备进行通信
  • 【sgFloatDialog】自定义组件:浮动弹窗,支持修改尺寸、拖拽位置、最大化、还原、最小化、复位
  • AdaWaveNet:用于时间序列分析的自适应小波网络
  • 高并发编程有哪些规范?
  • 群晖中的docker设置总是不生效,尤其代理
  • 天梯赛 L2-011 玩转二叉树
  • 【MyDB】7-客户端服务端通信之3-Client的实现
  • AI日报 - 2025年3月21日