【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}`)}
}