SqaueLink.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { _decorator, Component, EventTouch, instantiate, Node, Prefab, Vec2 } from 'cc';
  2. import { sqaueUltl } from '../SqaueUItl/sqaueUltl';
  3. const { ccclass, property } = _decorator;
  4. @ccclass('SqaueLink')
  5. export class SqaueLink extends Component {
  6. @property(Prefab)
  7. blockPrefab: Prefab = null; // 方块预制体
  8. private board: number[][] = []; // 游戏棋盘数据
  9. private blocks: Node[][] = []; // 游戏方块节点
  10. private selectedBlock: Vec2 = null; // 当前选中的方块
  11. start() {
  12. }
  13. init() {
  14. this.createBoard();
  15. this.createBlocks();
  16. }
  17. createBoard() {
  18. // 创建游戏棋盘数据
  19. for (let i = 0; i < sqaueUltl.ROW_NUM; i++) {
  20. this.board[i] = [];
  21. for (let j = 0; j < sqaueUltl.COL_NUM; j++) {
  22. this.board[i][j] = 1;
  23. }
  24. }
  25. }
  26. createBlocks() {
  27. const startX = -315;
  28. const startY = 40;
  29. // 初始化blocks数组
  30. this.blocks = [];
  31. for (let i = 0; i < sqaueUltl.ROW_NUM; i++) {
  32. this.blocks[i] = []; // 初始化每一行
  33. for (let j = 0; j < sqaueUltl.COL_NUM; j++) {
  34. // 如果棋盘上该位置为0,则不创建方块
  35. if (this.board[i][j] === 0) {
  36. this.blocks[i][j] = null; // 存储为null
  37. continue;
  38. }
  39. const tempBlock = instantiate(this.blockPrefab);
  40. const blockProto = tempBlock.getChildByName("icon");
  41. const posX = startX + i * sqaueUltl.spacing;
  42. const posY = startY - j * sqaueUltl.spacing;
  43. tempBlock.setPosition(posX, posY);
  44. this.node.addChild(tempBlock);
  45. // 存储方块节点
  46. this.blocks[i][j] = tempBlock;
  47. // 点击事件
  48. tempBlock.on(Node.EventType.TOUCH_END,this.onTouchStart,this);
  49. }
  50. }
  51. }
  52. onTouchStart(event: EventTouch)
  53. {
  54. }
  55. update(deltaTime: number) {
  56. }
  57. }