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

通信工程网站建设/郑州网络运营培训

通信工程网站建设,郑州网络运营培训,网站建设收费明细表,天津建网站的公司简介使用cocos creator2.x版本制作的拼图游戏, 移动图块, 还原最终的样子开始, 我们分析一下这个游戏的几个要点 1, 是如何将一张完整的图片分成3*3 5*5个小图, 并且这些小图要可以保存自己的位置信息, 等一些属性 2, 是如何表示小图合集的位置, 用什么数据结构保存, 且怎么让图…

简介使用cocos creator2.x版本制作的拼图游戏, 移动图块, 还原最终的样子

开始, 我们分析一下这个游戏的几个要点 1, 是如何将一张完整的图片分成3*3 5*5个小图, 并且这些小图要可以保存自己的位置信息, 等一些属性 2, 是如何表示小图合集的位置, 用什么数据结构保存, 且怎么让图片逻辑, 与数据逻辑对应 3, 是如何判断游戏结束

ffa5ab5b84e633900f992fac2fa53a95.png

上图是游戏的场景结构, 可以看到2.x版本和1.x版本有一些区别, 最突出的就是新增了一个默认的carmera结点 这个我会在别的帖子内仔细介绍, 这里不再多说

首先我们解决第一个问题, 如何将一个大图切成一个个小图, 并让这个小图保存一些属性值, 我们先新建一个脚本, puzzlePiece.ts,

  const { ccclass, property } = cc._decorator;@ccclassexport class Piece extends cc.Component { @property({ type: cc.Texture2D }) private texture: cc.Texture2D = null; public oriCol: number; public oriRow: number; public curCol: number; public curRow: number; public isBlank: boolean; public get isRight() { return this.curCol === this.oriCol && this.curRow === this.oriRow; } public init(col: number, row: number, colNum: number, colWidth: number) { this.oriCol = col; this.oriRow = row; this.curCol = col; this.curRow = row; let sprite = this.node.addComponent(cc.Sprite); // 升级2.0后setRect失效  // sprite.spriteFrame = new cc.SpriteFrame(this.texture); // let rect = sprite.spriteFrame.getRect(); let rect = cc.rect(0, 0, this.texture.width, this.texture.height); let newRectWidth = rect.width / colNum; let newRectHeight = rect.height / colNum; let newRectX = col * newRectWidth; let newRectY = (colNum - row - 1) * newRectHeight; let newRect = cc.rect(newRectX, newRectY, newRectWidth, newRectHeight); // sprite.spriteFrame.setRect(newRect); sprite.spriteFrame = new cc.SpriteFrame(this.texture, newRect); this.node.width = colWidth; this.node.height = colWidth; this.isBlank = this.oriCol === colNum - 1 && this.oriRow === 0; if (this.isBlank) { this.node.active = false; } }}  

将小图看做一个类, 使用texture保存图片纹理,在通过new cc.SpriteFrame(this.texture, newRect);获取某一个矩形区域内的纹理, 这样就可以把一张大图切成一张张小图, 并添加几个属性, 保存位置和其他的信息

那么开始解决第二个问题, 我们可以采取二维数组的数据结构保存数据信息private pieceMap: Array; 讲一个个切好的小图保存在内, 然后随机移动(为了保证图片可以还原, 我采取的方法是将一个正确摆放好的小图数组, 按照游戏规定的移动方式随机移动1000次), 这样图片一定可以被还原

  import { Piece } from "./PuzzlePiece";import { PuzzleScene } from "./PuzzleScene";const { ccclass, property, executeInEditMode } = cc._decorator;@ccclass// @executeInEditModeexport class PuzzleBoard extends cc.Component { @property(cc.Prefab) private piecePrefab: cc.Prefab = null; @property(cc.Integer) private colNum: number = 5; @property(cc.Integer) private colSpace: number = 5; private colWidth: number = 0; private pieceMap: Array; private blankPiece: Piece = null; private puzzleScene: PuzzleScene = null; init(puzzleScene: PuzzleScene) { this.puzzleScene = puzzleScene; this.addListeners(); } public reset(colNum?: number) { this.colNum = colNum; this.colWidth = (this.node.width - this.colSpace * (this.colNum + 1)) / this.colNum; this.node.removeAllChildren(); this.pieceMap = []; for (let x = 0; x < this.colNum; x++) { this.pieceMap[x] = []; for (let y = 0; y < this.colNum; y++) { let pieceNode = cc.instantiate(this.piecePrefab); this.node.addChild(pieceNode); pieceNode.x = x * (this.colWidth + this.colSpace) + this.colSpace; pieceNode.y = y * (this.colWidth + this.colSpace) + this.colSpace; this.pieceMap[x][y] = pieceNode.getComponent(Piece); this.pieceMap[x][y].init(x, y, this.colNum, this.colWidth); if (this.pieceMap[x][y].isBlank) { this.blankPiece = this.pieceMap[x][y]; } } } this.shuffle(); } private shuffle() { for (let i = 0; i < 1000; i++) { let nearPieces = this.getNearPieces(this.blankPiece); let n = Math.floor(Math.random() * nearPieces.length); this.exchangeTwoPiece(this.blankPiece, nearPieces[n]); } } private onBoadTouch(event: cc.Event.EventTouch) { let worldPos = event.getLocation(); let localPos = this.node.convertToNodeSpaceAR(worldPos); let x = Math.floor((localPos.x - this.colSpace) / (this.colWidth + this.colSpace)); let y = Math.floor((localPos.y - this.colSpace) / (this.colWidth + this.colSpace)); this.puzzleScene.onBoardTouch(x, y); } public movePiece(x, y): boolean { let piece = this.pieceMap[x][y]; let nearPieces = this.getNearPieces(piece); for (let nearPiece of nearPieces) { if (nearPiece.isBlank) { this.exchangeTwoPiece(piece, nearPiece); return true; } } return false; } public judgeWin(): boolean { for (let x = 0; x < this.colNum; x++) { for (let y = 0; y < this.colNum; y++) { if(!this.pieceMap[x][y].isRight) { return false; } } } this.blankPiece.node.active = true; return true; } private getNearPieces(piece: Piece): Array { let nearPieces = []; if (piece.curCol > 0) { // left nearPieces.push(this.pieceMap[piece.curCol - 1][piece.curRow]); } if (piece.curCol < this.colNum - 1) { // right nearPieces.push(this.pieceMap[piece.curCol + 1][piece.curRow]); } if (piece.curRow > 0) { // bottom nearPieces.push(this.pieceMap[piece.curCol][piece.curRow - 1]); } if (piece.curRow < this.colNum - 1) { // top nearPieces.push(this.pieceMap[piece.curCol][piece.curRow + 1]); } return nearPieces; } public exchangeTwoPiece(piece1: Piece, piece2: Piece) { this.pieceMap[piece2.curCol][piece2.curRow] = piece1; this.pieceMap[piece1.curCol][piece1.curRow] = piece2; [piece1.curCol, piece2.curCol] = [piece2.curCol, piece1.curCol]; [piece1.curRow, piece2.curRow] = [piece2.curRow, piece1.curRow]; [piece1.node.position, piece2.node.position] = [piece2.node.position, piece1.node.position]; } private addListeners() { this.node.on(cc.Node.EventType.TOUCH_END, this.onBoadTouch, this); } private removeListeners() { }} 

解决第三个问题, 其实这个很简单, 因为我们已经在小图类中保存了小图本身的位置信息, 我们只需要,每次移动图片 都遍历一个二维数组判断其是否在正确的位置, 判断成功, 结束游戏

9491dd689d33fae39ccbae22e06c295f.gif

作者:Rainy_Night

http://www.jmfq.cn/news/5084749.html

相关文章:

  • 湖北省住房和城乡建设厅门户网站/郑州seo多少钱
  • 曰本真人性做爰 酥酥网站/百度网络营销中心
  • 深圳网站设计公司电/热搜榜排名今日
  • 做微信网站/线上宣传方式
  • 电商实训网站建设报告/百度推广技巧方法
  • 网站建设公司哪里好/线下营销推广方式有哪些
  • 新民正规网站建设价格咨询/网络广告四个特征
  • 西安网站制作的公司/网站运营师
  • 旅游加盟网站建设/江苏网站开发
  • 北京装修公司网站建设/电商网站对比表格
  • 一区适合晚上一个人看b站/免费优化网站
  • linux系统搭建网站/seo站长工具平台
  • 什么主题的网站容易做/百度小说官网
  • 方庄网站建设/补习班
  • 安徽设计网站建设/百度推广客户端电脑版
  • 触屏版网站模板/云南网站seo服务
  • 做网站骗/快排seo软件
  • 做资源网站违法吗/seo博客网址
  • 网站在互联网营销中的作用/常见的营销方式有哪些
  • 品牌网站官网/网络营销有哪些功能
  • 网站自动采集更新/今日国内新闻10则
  • 新疆网站备案/百度正版下载恢复百度
  • 应式网站/网站维护合同
  • php 企业建站cms/seo整站优化报价
  • 建立网站的流程多少钱/网站怎么快速排名
  • 梧州网站设计制作服务至上/离我最近的广告公司
  • 贵阳专业做网站的公司/海淀区seo搜索引擎优化企业
  • 国外做二手服装网站/提升seo排名平台
  • 如何开发微信微网站/河南新闻头条最新消息
  • 资料库网站应该怎么做/qq推广