import { _decorator, Component, EventTouch, instantiate, Node, Prefab, Vec2 } from 'cc'; import { sqaueUltl } from '../SqaueUItl/sqaueUltl'; const { ccclass, property } = _decorator; @ccclass('SqaueLink') export class SqaueLink extends Component { @property(Prefab) blockPrefab: Prefab = null; // 方块预制体 private board: number[][] = []; // 游戏棋盘数据 private blocks: Node[][] = []; // 游戏方块节点 private selectedBlock: Vec2 = null; // 当前选中的方块 start() { } init() { this.createBoard(); this.createBlocks(); } createBoard() { // 创建游戏棋盘数据 for (let i = 0; i < sqaueUltl.ROW_NUM; i++) { this.board[i] = []; for (let j = 0; j < sqaueUltl.COL_NUM; j++) { this.board[i][j] = 1; } } } createBlocks() { const startX = -315; const startY = 40; // 初始化blocks数组 this.blocks = []; for (let i = 0; i < sqaueUltl.ROW_NUM; i++) { this.blocks[i] = []; // 初始化每一行 for (let j = 0; j < sqaueUltl.COL_NUM; j++) { // 如果棋盘上该位置为0,则不创建方块 if (this.board[i][j] === 0) { this.blocks[i][j] = null; // 存储为null continue; } const tempBlock = instantiate(this.blockPrefab); const blockProto = tempBlock.getChildByName("icon"); const posX = startX + i * sqaueUltl.spacing; const posY = startY - j * sqaueUltl.spacing; tempBlock.setPosition(posX, posY); this.node.addChild(tempBlock); // 存储方块节点 this.blocks[i][j] = tempBlock; // 点击事件 tempBlock.on(Node.EventType.TOUCH_END,this.onTouchStart,this); } } } onTouchStart(event: EventTouch) { } update(deltaTime: number) { } }