0


基于js原生算法+cocos游戏引擎+uni框架Cloud托管网页:开发2048小游戏域名发布版本

首先看一下效果图

CocosCreator游戏引擎

安装完成之后创建一个空项目:

我这里创建了一个,我就直接用了!

首先我们先创建三个文件夹:

首先我们在scenes里面创建一个场景:并起名为:game

然后我们在创建两个单色节点,其中这两个单色节点右包括三个文本节点!

block里面的绑定:

注意:绑定要在写完js代码之后才可以绑定!所以建议先看下面的js代码及算法代码!再回头来绑定!

block绑定:

block绑定完成之后我们可以直接把整个总结点拖入这个文件夹下,让其当作预备节点存储,随时使用!(如下图所示)

这是我们就可以把面板中的block总结点删除了:

** canvas绑定:**

cocos中创建脚本js以及js原生算法

我们需要在script里面创建三个js脚本

创建方法如下:

然后我们一次用vscode打开,当然其他的编辑器也可以,当时本人习惯用vscode,所以这里就拿它来演示了~

我们右键点击他们三个时,选择从"资源浏览器中显示"(如下图)

会发现每一个js文件都有一个对应的meta文件,这说明我们每一个js文件创建的都非常成功!

然后我们反别用vscode打开:

然后我们先看game.js

properties里面是你上面所说的,注册的节点绑定!

  1. properties: {
  2. scoreLabel:cc.Label,
  3. score:0,
  4. blockPrefab:cc.Prefab,
  5. gap:10,
  6. bg:cc.Node,
  7. // GameOver:cc.Label
  8. },

发现所有我们上面显示的节点都有在这里注册!注册完成之后我们再去按照上面所述绑定即可!

start里面是我们在下面要写的方法,在写之前要在这里自定义方法!

  1. start () {
  2. this.drawBgBlocks();
  3. this.init();
  4. this.addEventHandler();
  5. },

第一步我们需要构思一下,我们需要一些什么固定的常量:

1.需要一个表示几行几列的常量

2.需要一个初始化时所及定义数字大小范围的常量

3.需要一个判断一个手指滑动鉴定长度开始判定执行的常量

4.需要一个当数字块滑动时延迟滑行的常量

常量如下所示:

  1. //调整几*几
  2. const ROWS = 4;
  3. //添加的数字大小
  4. const NUMBERS = [2,4];
  5. //判断一个手指滑动鉴定长度开始判定执行
  6. const MIN_LENGTH = 50;
  7. //当数字块滑动时延迟滑行
  8. const MOVE_DURATION = 0.08;

** start用来定义下面所需要的用到的方法,如本次用到的方法:**

1.处理绘制背景的方法

2.处理游戏开始的方法

3.处理监听事件的方法

  1. start () {
  2. this.drawBgBlocks();
  3. this.init();
  4. this.addEventHandler();
  5. },

背景方法及包含的原生算法代码:

  1. drawBgBlocks(){
  2. this.blockSize = (cc.winSize.width - this.gap * (ROWS+1)) / ROWS;
  3. let x = this.gap + this.blockSize /2;
  4. let y = this.blockSize;
  5. this.positions = [];
  6. for(let i=0;i<ROWS;++i){
  7. this.positions.push([0,0,0,0]);
  8. for(let j=0;j<ROWS;++j){
  9. let block = cc.instantiate(this.blockPrefab);
  10. block.width = this.blockSize;
  11. block.height = this.blockSize;
  12. this.bg.addChild(block);
  13. block.setPosition(cc.p(x,y));
  14. this.positions[i][j] = cc.p(x,y);
  15. x += this.gap + this.blockSize;
  16. block.getComponent('block').setNumber(0);
  17. }
  18. y += this.gap + this.blockSize;
  19. x = this.gap + this.blockSize / 2;
  20. }
  21. },

开始方法及包含的原生算法代码:

  1. init(){
  2. this.updateScore(0);
  3. if (this.blocks){
  4. for(let i=0;i<this.blocks.length;++i){
  5. for(let j=0;j<this.blocks[i].length;++j){
  6. if (this.blocks [i][j] != null){
  7. this.blocks[i][j].destroy();
  8. }
  9. }
  10. }
  11. }
  12. this.data = [];
  13. this.blocks = [];
  14. for(let i=0;i<ROWS;++i){
  15. this.blocks.push([null,null,null,null]);
  16. this.data.push([0,0,0,0]);
  17. }
  18. this.addBlock();
  19. this.addBlock();
  20. this.addBlock();
  21. },

处理监听事件的方法及包含的原生算法代码:

  1. addEventHandler(){
  2. this.bg.on('touchstart',(event)=>{
  3. this.startPoint = event.getLocation();
  4. });
  5. this.bg.on('touchend',(event)=>{
  6. this.touchEnd(event);
  7. });
  8. this.bg.on('touchcancel',(event)=>{
  9. this.touchEnd(event);
  10. });
  11. },
  12. touchEnd(event){
  13. this.endPoint = event.getLocation();
  14. let vec = cc.pSub(this.endPoint,this.startPoint);
  15. if (cc.pLength(vec) > MIN_LENGTH) {
  16. if(Math.abs(vec.x) > Math.abs(vec.y)){
  17. //水平方向
  18. if(vec.x > 0){
  19. this.moveRight();
  20. }else{
  21. this.moveLeft();
  22. }
  23. } else{
  24. //竖直方向
  25. if(vec.y > 0){
  26. this.moveUp();
  27. }else{
  28. this.moveDown();
  29. }
  30. }
  31. }
  32. },
  33. checkFail(){
  34. for (let i=0; i<ROWS;++i){
  35. for(let j=0;j<ROWS;++j){
  36. let n = this.data[i][j];
  37. if(n == 0) return false;
  38. if(j>0 && this.data[i][j-1] == n) return false;
  39. if(j<3 && this.data[i][j+1] == n) return false;
  40. if(i>0 && this.data[i-1][j] == n) return false;
  41. if(i<3 && this.data[i+1][j] == n) return false;
  42. }
  43. }
  44. return true;
  45. },
  46. GameOver(){
  47. cc.log('game over!')
  48. // this.GameOver.string = '游戏结束!!!'
  49. },
  50. afterMove(hasMove){
  51. if(hasMove){
  52. this.updateScore(this.score+1)
  53. this.addBlock();
  54. }
  55. if(this.checkFail()){
  56. this.GameOver();
  57. }
  58. },
  59. //移动格子
  60. doMove(block,position,callback){
  61. let action = cc.moveTo(MOVE_DURATION,position);
  62. let finish = cc.callFunc(()=>{
  63. callback && callback()
  64. });
  65. block.runAction(cc.sequence(action,finish));
  66. },
  67. moveLeft(){
  68. cc.log('moveLeft');
  69. let hasMove = false;
  70. let move = (x,y,callback) =>{
  71. if(y == 0 || this.data[x][y] == 0){
  72. cc.log('case 1');
  73. callback && callback();
  74. return;
  75. }else if(this.data[x][y-1] == 0){
  76. //移动
  77. // cc.log('case 2');
  78. let block = this.blocks[x][y];
  79. let position = this.positions[x][y-1];
  80. this.blocks[x][y-1] = block;
  81. this.data[x][y-1] = this.data[x][y];
  82. this.data[x][y] = 0;
  83. this.blocks[x][y] = null;
  84. this.doMove(block,position,()=>{
  85. move(x,y-1,callback);
  86. })
  87. hasMove = true;
  88. }else if (this.data[x][y-1] == this.data[x][y]){
  89. //合并
  90. // cc.log('case 3');
  91. let block = this.blocks[x][y];
  92. let position = this.positions[x][y-1];
  93. this.data[x][y-1] *= 2;
  94. this.data[x][y] = 0;
  95. this.blocks[x][y] = null;
  96. this.blocks[x][y-1].getComponent('block').setNumber(this.data[x][y-1]);
  97. this.doMove(block,position,()=>{
  98. block.destroy();
  99. callback && callback();
  100. });
  101. hasMove = true;
  102. }else{
  103. callback && callback();
  104. cc.log('case 4');
  105. return;
  106. }
  107. };
  108. let toMove = [];
  109. for (let i=0;i<ROWS;++i){
  110. for(let j=0;j<ROWS;++j){
  111. if(this.data[i][j] !=0){
  112. toMove.push({x:i,y:j});
  113. }
  114. }
  115. }
  116. let counter = 0;
  117. for (let i=0;i<toMove.length;++i) {
  118. move(toMove[i].x,toMove[i].y,()=>{
  119. counter++;
  120. if(counter == toMove.length){
  121. this.afterMove(hasMove);
  122. }
  123. });
  124. }
  125. },
  126. moveRight(){
  127. cc.log('moveRight');
  128. let hasMove = false;
  129. let move = (x,y,callback) =>{
  130. if(y == 3 || this.data[x][y] == 0){
  131. cc.log('case 1');
  132. callback && callback();
  133. return;
  134. }else if(this.data[x][y+1] == 0){
  135. //移动
  136. // cc.log('case 2');
  137. let block = this.blocks[x][y];
  138. let position = this.positions[x][y+1];
  139. this.blocks[x][y+1] = block;
  140. this.data[x][y+1] = this.data[x][y];
  141. this.data[x][y] = 0;
  142. this.blocks[x][y] = null;
  143. this.doMove(block,position,()=>{
  144. move(x,y+1,callback);
  145. })
  146. hasMove = true;
  147. }else if (this.data[x][y+1] == this.data[x][y]){
  148. //合并
  149. // cc.log('case 3');
  150. let block = this.blocks[x][y];
  151. let position = this.positions[x][y+1];
  152. this.data[x][y+1] *= 2;
  153. this.data[x][y] = 0;
  154. this.blocks[x][y] = null;
  155. this.blocks[x][y+1].getComponent('block').setNumber(this.data[x][y+1]);
  156. this.doMove(block,position,()=>{
  157. block.destroy();
  158. callback && callback();
  159. });
  160. hasMove = true;
  161. }else{
  162. callback && callback();
  163. cc.log('case 4');
  164. return;
  165. }
  166. };
  167. let toMove = [];
  168. for (let i=0;i<ROWS;++i){
  169. for(let j=ROWS-1;j>=0;--j){
  170. if(this.data[i][j] !=0){
  171. toMove.push({x:i,y:j});
  172. }
  173. }
  174. }
  175. let counter = 0;
  176. for (let i=0;i<toMove.length;++i) {
  177. move(toMove[i].x,toMove[i].y,()=>{
  178. counter++;
  179. if(counter == toMove.length){
  180. this.afterMove(hasMove);
  181. }
  182. });
  183. }
  184. },
  185. moveUp(){
  186. cc.log('move Up');
  187. let hasMove = false;
  188. let move = (x,y,callback) =>{
  189. if(x == 3 || this.data[x][y] == 0){
  190. cc.log('case 1');
  191. callback && callback();
  192. return;
  193. }else if(this.data[x+1][y] == 0){
  194. //移动
  195. // cc.log('case 2');
  196. let block = this.blocks[x][y];
  197. let position = this.positions[x+1][y];
  198. this.blocks[x+1][y] = block;
  199. this.data[x+1][y] = this.data[x][y];
  200. this.data[x][y] = 0;
  201. this.blocks[x][y] = null;
  202. this.doMove(block,position,()=>{
  203. move(x+1,y,callback);
  204. })
  205. hasMove = true;
  206. }else if (this.data[x+1][y] == this.data[x][y]){
  207. //合并
  208. // cc.log('case 3');
  209. let block = this.blocks[x][y];
  210. let position = this.positions[x+1][y];
  211. this.data[x+1][y] *= 2;
  212. this.data[x][y] = 0;
  213. this.blocks[x][y] = null;
  214. this.blocks[x+1][y].getComponent('block').setNumber(this.data[x+1][y]);
  215. this.doMove(block,position,()=>{
  216. block.destroy();
  217. callback && callback();
  218. });
  219. hasMove = true;
  220. }else{
  221. callback && callback();
  222. cc.log('case 4');
  223. return;
  224. }
  225. };
  226. let toMove = [];
  227. for (let i=3;i>=0;--i){
  228. for(let j=0;j<ROWS;++j){
  229. if(this.data[i][j] !=0){
  230. toMove.push({x:i,y:j});
  231. }
  232. }
  233. }
  234. let counter = 0;
  235. for (let i=0;i<toMove.length;++i) {
  236. move(toMove[i].x,toMove[i].y,()=>{
  237. counter++;
  238. if(counter == toMove.length){
  239. this.afterMove(hasMove);
  240. }
  241. });
  242. }
  243. },
  244. moveDown(){
  245. cc.log('move Down');
  246. let hasMove = false;
  247. let move = (x,y,callback) =>{
  248. if(x == 0 || this.data[x][y] == 0){
  249. cc.log('case 1');
  250. callback && callback();
  251. return;
  252. }else if(this.data[x-1][y] == 0){
  253. //移动
  254. // cc.log('case 2');
  255. let block = this.blocks[x][y];
  256. let position = this.positions[x-1][y];
  257. this.blocks[x-1][y] = block;
  258. this.data[x-1][y] = this.data[x][y];
  259. this.data[x][y] = 0;
  260. this.blocks[x][y] = null;
  261. this.doMove(block,position,()=>{
  262. move(x-1,y,callback);
  263. })
  264. hasMove = true;
  265. }else if (this.data[x-1][y] == this.data[x][y]){
  266. //合并
  267. // cc.log('case 3');
  268. let block = this.blocks[x][y];
  269. let position = this.positions[x-1][y];
  270. this.data[x-1][y] *= 2;
  271. this.data[x][y] = 0;
  272. this.blocks[x][y] = null;
  273. this.blocks[x-1][y].getComponent('block').setNumber(this.data[x-1][y]);
  274. this.doMove(block,position,()=>{
  275. block.destroy();
  276. callback && callback();
  277. });
  278. hasMove = true;
  279. }else{
  280. callback && callback();
  281. cc.log('case 4');
  282. return;
  283. }
  284. };
  285. let toMove = [];
  286. for (let i=0;i<ROWS;++i){
  287. for(let j=0;j<ROWS;++j){
  288. if(this.data[i][j] !=0){
  289. toMove.push({x:i,y:j});
  290. }
  291. }
  292. }
  293. let counter = 0;
  294. for (let i=0;i<toMove.length;++i) {
  295. move(toMove[i].x,toMove[i].y,()=>{
  296. counter++;
  297. if(counter == toMove.length){
  298. this.afterMove(hasMove);
  299. }
  300. });
  301. }
  302. }

game.js完整代码:

  1. //调整几*几
  2. const ROWS = 4;
  3. //添加的数字大小
  4. const NUMBERS = [2,4];
  5. //判断一个手指滑动鉴定长度开始判定执行
  6. const MIN_LENGTH = 50;
  7. //当数字块滑动时延迟滑行
  8. const MOVE_DURATION = 0.08;
  9. cc.Class({
  10. extends: cc.Component,
  11. properties: {
  12. scoreLabel:cc.Label,
  13. score:0,
  14. blockPrefab:cc.Prefab,
  15. gap:10,
  16. bg:cc.Node,
  17. // GameOver:cc.Label
  18. },
  19. // LIFE-CYCLE CALLBACKS:
  20. // onLoad () {},
  21. start () {
  22. this.drawBgBlocks();
  23. this.init();
  24. this.addEventHandler();
  25. },
  26. drawBgBlocks(){
  27. this.blockSize = (cc.winSize.width - this.gap * (ROWS+1)) / ROWS;
  28. let x = this.gap + this.blockSize /2;
  29. let y = this.blockSize;
  30. this.positions = [];
  31. for(let i=0;i<ROWS;++i){
  32. this.positions.push([0,0,0,0]);
  33. for(let j=0;j<ROWS;++j){
  34. let block = cc.instantiate(this.blockPrefab);
  35. block.width = this.blockSize;
  36. block.height = this.blockSize;
  37. this.bg.addChild(block);
  38. block.setPosition(cc.p(x,y));
  39. this.positions[i][j] = cc.p(x,y);
  40. x += this.gap + this.blockSize;
  41. block.getComponent('block').setNumber(0);
  42. }
  43. y += this.gap + this.blockSize;
  44. x = this.gap + this.blockSize / 2;
  45. }
  46. },
  47. // drawBgBlocks(){
  48. // this.blockSize = [cc.winSize.width - this.gap * (ROWS+1)] / ROWS;
  49. // let x = this.gap + this.blockSize / 2;
  50. // let y = this.blockSize;
  51. // this.positions = [];
  52. // for (let i=0;i<ROWS;i++) {
  53. // this.positions.push([0,0,0,0]);
  54. // for(let j=0;j<ROWS;++j){
  55. // let block = cc.instantiate(this.blockPrefab);
  56. // block.width = this.blockSize;
  57. // block.height = this.blockSize;
  58. // this.bg.addChild(block);
  59. // block.setPosition(cc.v2(x,y));
  60. // this.positions[i][j] = cc.v2(x,y);
  61. // x += this.gap + this.blockSize;
  62. // block.getComponent('block').setNumber(0);
  63. // }
  64. // y += this.gap + this.blockSize;
  65. // x = this.gap + this.blockSize / 2;
  66. // }
  67. // cc.log(this.positions)
  68. // },
  69. init(){
  70. this.updateScore(0);
  71. if (this.blocks){
  72. for(let i=0;i<this.blocks.length;++i){
  73. for(let j=0;j<this.blocks[i].length;++j){
  74. if (this.blocks [i][j] != null){
  75. this.blocks[i][j].destroy();
  76. }
  77. }
  78. }
  79. }
  80. this.data = [];
  81. this.blocks = [];
  82. for(let i=0;i<ROWS;++i){
  83. this.blocks.push([null,null,null,null]);
  84. this.data.push([0,0,0,0]);
  85. }
  86. this.addBlock();
  87. this.addBlock();
  88. this.addBlock();
  89. },
  90. updateScore(number) {
  91. this.score = number;
  92. this.scoreLabel.string = '滑动得分:' + number;
  93. },
  94. // 找出空闲的块
  95. // 空闲块的位置表示
  96. getEmptyLocations(){
  97. let locations = [];
  98. for (let i=0; i<this.blocks.length;++i){
  99. for(let j=0;j<this.blocks[i].length;++j){
  100. if (this.blocks[i][j] == null) {
  101. locations.push({x:i,y:j});
  102. }
  103. }
  104. }
  105. return locations;
  106. },
  107. addBlock(){
  108. let locations = this.getEmptyLocations();
  109. if(locations.length == 0) return false;
  110. let location = locations[Math.floor(cc.random0To1() * locations.length)];
  111. let x = location.x;
  112. let y = location.y;
  113. let position = this.positions[x][y];
  114. let block = cc.instantiate(this.blockPrefab);
  115. block.width = this.blockSize;
  116. block.height = this.blockSize;
  117. this.bg.addChild(block);
  118. block.setPosition(position);
  119. let number = NUMBERS[Math.floor(cc.random0To1() * NUMBERS.length)]
  120. block.getComponent('block').setNumber(number);
  121. this.blocks[x][y] = block;
  122. this.data[x][y] = number;
  123. return true;
  124. // cc.log(locations);
  125. },
  126. addEventHandler(){
  127. this.bg.on('touchstart',(event)=>{
  128. this.startPoint = event.getLocation();
  129. });
  130. this.bg.on('touchend',(event)=>{
  131. this.touchEnd(event);
  132. });
  133. this.bg.on('touchcancel',(event)=>{
  134. this.touchEnd(event);
  135. });
  136. },
  137. touchEnd(event){
  138. this.endPoint = event.getLocation();
  139. let vec = cc.pSub(this.endPoint,this.startPoint);
  140. if (cc.pLength(vec) > MIN_LENGTH) {
  141. if(Math.abs(vec.x) > Math.abs(vec.y)){
  142. //水平方向
  143. if(vec.x > 0){
  144. this.moveRight();
  145. }else{
  146. this.moveLeft();
  147. }
  148. } else{
  149. //竖直方向
  150. if(vec.y > 0){
  151. this.moveUp();
  152. }else{
  153. this.moveDown();
  154. }
  155. }
  156. }
  157. },
  158. checkFail(){
  159. for (let i=0; i<ROWS;++i){
  160. for(let j=0;j<ROWS;++j){
  161. let n = this.data[i][j];
  162. if(n == 0) return false;
  163. if(j>0 && this.data[i][j-1] == n) return false;
  164. if(j<3 && this.data[i][j+1] == n) return false;
  165. if(i>0 && this.data[i-1][j] == n) return false;
  166. if(i<3 && this.data[i+1][j] == n) return false;
  167. }
  168. }
  169. return true;
  170. },
  171. GameOver(){
  172. cc.log('game over!')
  173. // this.GameOver.string = '游戏结束!!!'
  174. },
  175. afterMove(hasMove){
  176. if(hasMove){
  177. this.updateScore(this.score+1)
  178. this.addBlock();
  179. }
  180. if(this.checkFail()){
  181. this.GameOver();
  182. }
  183. },
  184. //移动格子
  185. doMove(block,position,callback){
  186. let action = cc.moveTo(MOVE_DURATION,position);
  187. let finish = cc.callFunc(()=>{
  188. callback && callback()
  189. });
  190. block.runAction(cc.sequence(action,finish));
  191. },
  192. moveLeft(){
  193. cc.log('moveLeft');
  194. let hasMove = false;
  195. let move = (x,y,callback) =>{
  196. if(y == 0 || this.data[x][y] == 0){
  197. cc.log('case 1');
  198. callback && callback();
  199. return;
  200. }else if(this.data[x][y-1] == 0){
  201. //移动
  202. // cc.log('case 2');
  203. let block = this.blocks[x][y];
  204. let position = this.positions[x][y-1];
  205. this.blocks[x][y-1] = block;
  206. this.data[x][y-1] = this.data[x][y];
  207. this.data[x][y] = 0;
  208. this.blocks[x][y] = null;
  209. this.doMove(block,position,()=>{
  210. move(x,y-1,callback);
  211. })
  212. hasMove = true;
  213. }else if (this.data[x][y-1] == this.data[x][y]){
  214. //合并
  215. // cc.log('case 3');
  216. let block = this.blocks[x][y];
  217. let position = this.positions[x][y-1];
  218. this.data[x][y-1] *= 2;
  219. this.data[x][y] = 0;
  220. this.blocks[x][y] = null;
  221. this.blocks[x][y-1].getComponent('block').setNumber(this.data[x][y-1]);
  222. this.doMove(block,position,()=>{
  223. block.destroy();
  224. callback && callback();
  225. });
  226. hasMove = true;
  227. }else{
  228. callback && callback();
  229. cc.log('case 4');
  230. return;
  231. }
  232. };
  233. let toMove = [];
  234. for (let i=0;i<ROWS;++i){
  235. for(let j=0;j<ROWS;++j){
  236. if(this.data[i][j] !=0){
  237. toMove.push({x:i,y:j});
  238. }
  239. }
  240. }
  241. let counter = 0;
  242. for (let i=0;i<toMove.length;++i) {
  243. move(toMove[i].x,toMove[i].y,()=>{
  244. counter++;
  245. if(counter == toMove.length){
  246. this.afterMove(hasMove);
  247. }
  248. });
  249. }
  250. },
  251. moveRight(){
  252. cc.log('moveRight');
  253. let hasMove = false;
  254. let move = (x,y,callback) =>{
  255. if(y == 3 || this.data[x][y] == 0){
  256. cc.log('case 1');
  257. callback && callback();
  258. return;
  259. }else if(this.data[x][y+1] == 0){
  260. //移动
  261. // cc.log('case 2');
  262. let block = this.blocks[x][y];
  263. let position = this.positions[x][y+1];
  264. this.blocks[x][y+1] = block;
  265. this.data[x][y+1] = this.data[x][y];
  266. this.data[x][y] = 0;
  267. this.blocks[x][y] = null;
  268. this.doMove(block,position,()=>{
  269. move(x,y+1,callback);
  270. })
  271. hasMove = true;
  272. }else if (this.data[x][y+1] == this.data[x][y]){
  273. //合并
  274. // cc.log('case 3');
  275. let block = this.blocks[x][y];
  276. let position = this.positions[x][y+1];
  277. this.data[x][y+1] *= 2;
  278. this.data[x][y] = 0;
  279. this.blocks[x][y] = null;
  280. this.blocks[x][y+1].getComponent('block').setNumber(this.data[x][y+1]);
  281. this.doMove(block,position,()=>{
  282. block.destroy();
  283. callback && callback();
  284. });
  285. hasMove = true;
  286. }else{
  287. callback && callback();
  288. cc.log('case 4');
  289. return;
  290. }
  291. };
  292. let toMove = [];
  293. for (let i=0;i<ROWS;++i){
  294. for(let j=ROWS-1;j>=0;--j){
  295. if(this.data[i][j] !=0){
  296. toMove.push({x:i,y:j});
  297. }
  298. }
  299. }
  300. let counter = 0;
  301. for (let i=0;i<toMove.length;++i) {
  302. move(toMove[i].x,toMove[i].y,()=>{
  303. counter++;
  304. if(counter == toMove.length){
  305. this.afterMove(hasMove);
  306. }
  307. });
  308. }
  309. },
  310. moveUp(){
  311. cc.log('move Up');
  312. let hasMove = false;
  313. let move = (x,y,callback) =>{
  314. if(x == 3 || this.data[x][y] == 0){
  315. cc.log('case 1');
  316. callback && callback();
  317. return;
  318. }else if(this.data[x+1][y] == 0){
  319. //移动
  320. // cc.log('case 2');
  321. let block = this.blocks[x][y];
  322. let position = this.positions[x+1][y];
  323. this.blocks[x+1][y] = block;
  324. this.data[x+1][y] = this.data[x][y];
  325. this.data[x][y] = 0;
  326. this.blocks[x][y] = null;
  327. this.doMove(block,position,()=>{
  328. move(x+1,y,callback);
  329. })
  330. hasMove = true;
  331. }else if (this.data[x+1][y] == this.data[x][y]){
  332. //合并
  333. // cc.log('case 3');
  334. let block = this.blocks[x][y];
  335. let position = this.positions[x+1][y];
  336. this.data[x+1][y] *= 2;
  337. this.data[x][y] = 0;
  338. this.blocks[x][y] = null;
  339. this.blocks[x+1][y].getComponent('block').setNumber(this.data[x+1][y]);
  340. this.doMove(block,position,()=>{
  341. block.destroy();
  342. callback && callback();
  343. });
  344. hasMove = true;
  345. }else{
  346. callback && callback();
  347. cc.log('case 4');
  348. return;
  349. }
  350. };
  351. let toMove = [];
  352. for (let i=3;i>=0;--i){
  353. for(let j=0;j<ROWS;++j){
  354. if(this.data[i][j] !=0){
  355. toMove.push({x:i,y:j});
  356. }
  357. }
  358. }
  359. let counter = 0;
  360. for (let i=0;i<toMove.length;++i) {
  361. move(toMove[i].x,toMove[i].y,()=>{
  362. counter++;
  363. if(counter == toMove.length){
  364. this.afterMove(hasMove);
  365. }
  366. });
  367. }
  368. },
  369. moveDown(){
  370. cc.log('move Down');
  371. let hasMove = false;
  372. let move = (x,y,callback) =>{
  373. if(x == 0 || this.data[x][y] == 0){
  374. cc.log('case 1');
  375. callback && callback();
  376. return;
  377. }else if(this.data[x-1][y] == 0){
  378. //移动
  379. // cc.log('case 2');
  380. let block = this.blocks[x][y];
  381. let position = this.positions[x-1][y];
  382. this.blocks[x-1][y] = block;
  383. this.data[x-1][y] = this.data[x][y];
  384. this.data[x][y] = 0;
  385. this.blocks[x][y] = null;
  386. this.doMove(block,position,()=>{
  387. move(x-1,y,callback);
  388. })
  389. hasMove = true;
  390. }else if (this.data[x-1][y] == this.data[x][y]){
  391. //合并
  392. // cc.log('case 3');
  393. let block = this.blocks[x][y];
  394. let position = this.positions[x-1][y];
  395. this.data[x-1][y] *= 2;
  396. this.data[x][y] = 0;
  397. this.blocks[x][y] = null;
  398. this.blocks[x-1][y].getComponent('block').setNumber(this.data[x-1][y]);
  399. this.doMove(block,position,()=>{
  400. block.destroy();
  401. callback && callback();
  402. });
  403. hasMove = true;
  404. }else{
  405. callback && callback();
  406. cc.log('case 4');
  407. return;
  408. }
  409. };
  410. let toMove = [];
  411. for (let i=0;i<ROWS;++i){
  412. for(let j=0;j<ROWS;++j){
  413. if(this.data[i][j] !=0){
  414. toMove.push({x:i,y:j});
  415. }
  416. }
  417. }
  418. let counter = 0;
  419. for (let i=0;i<toMove.length;++i) {
  420. move(toMove[i].x,toMove[i].y,()=>{
  421. counter++;
  422. if(counter == toMove.length){
  423. this.afterMove(hasMove);
  424. }
  425. });
  426. }
  427. }
  428. // addEventHandler(){
  429. // this.bg.on('touchstart',(event)=>{
  430. // this.startPoint = event.getLocation();
  431. // });
  432. // this.bg.on('touchend',(event)=>{
  433. // this.endPoint = event.getLocation();
  434. // });
  435. // // this.bg.on('touchcancel',(event)=>{
  436. // // this.touchEnd(event);
  437. // // });
  438. // let vec = cc.pSub(this.endPoint,this.startPoint);
  439. // if (cc.pLength(vec) > MIN_LENGTH) {
  440. // if(Math.abs(vec.x) > Math.abs(vec.y)){
  441. // //水平方向
  442. // if(vec.x > 0){
  443. // this.moveRight();
  444. // }else{
  445. // this.moveLeft();
  446. // }
  447. // } else{
  448. // //竖直方向
  449. // if(vec.y > 0){
  450. // this.moveUp();
  451. // }else{
  452. // this.moveDown();
  453. // }
  454. // }
  455. // }
  456. // },
  457. // moveRight(){
  458. // cc.log('moveRight');
  459. // },
  460. // moveLeft(){
  461. // cc.log('moveLeft');
  462. // },
  463. // moveUp(){
  464. // cc.log('move Up');
  465. // },
  466. // moveDown(){
  467. // cc.log('move Down');
  468. // }
  469. // touchEnd(event){
  470. // this.endPoint = event.getLocation();
  471. // }
  472. // },
  473. // // update (dt) {},
  474. });

colors.js用来存储我们将会用到的块变化时以及背景的颜色

  1. var colors = [];
  2. colors[0] = cc.color(198,184,172,255);
  3. colors[2] = cc.color(235,224,213,255);
  4. colors[4] = cc.color(234,219,193,255);
  5. colors[8] = cc.color(240,167,110,255);
  6. colors[16] = cc.color(244,138,89,255);
  7. colors[32] = cc.color(245,112,85,255);
  8. colors[64] = cc.color(245,83,52,255);
  9. colors[128] = cc.color(234,200,103,255);
  10. colors[256] = cc.color(234,197,87,255);
  11. colors[512] = cc.color(234,192,71,255);
  12. colors[1024] = cc.color(146,208,80,255);
  13. colors[2048] = cc.color(0,176,240,255);
  14. module.exports = colors;

block.js是用来判定快与块合并时发生的颜色变化

  1. import colors from 'colors'
  2. cc.Class({
  3. extends: cc.Component,
  4. properties: {
  5. numberLabel:cc.Label
  6. },
  7. // LIFE-CYCLE CALLBACKS:
  8. // onLoad () {},
  9. start () {
  10. },
  11. setNumber(number){
  12. if(number == 0){
  13. this.numberLabel.node.active = false;
  14. }
  15. this.numberLabel.string = number;
  16. if (number in colors) {
  17. this.node.color = colors[number];
  18. }
  19. }
  20. // update (dt) {},
  21. });

最后我们在

用浏览器来预览一下我们写完的游戏

并打开开发者工具,每一个操作都会对应的打印该响应操作数据!

此时我们的代码算法及节点创建及游戏布局已经都已搭建成功,下面我们需要将本游戏打包:

在“项目”里面点击“构建发布”

之后会弹出:

** 我们选择前面带web的任意一个,一个是移动端,一个是页面桌面**

然后我们点击“构建”后点击“运行”

cocos打包完成后会在下面的控制台中显示打包成功后的路径!

uni框架Cloud托管网页

我们虽然写完了,而且各种功能也实现了,但是怎么样让我们的小游戏让别人也能通过一个网址的方式打开后玩耍呢?

当然大家可能第一反应就是购买服务器,买域名等等。。。

但是对于一个学生党来说,这中不必要的成本,只会让我们本不富裕的学生生活雪上加霜!!!

所以我们可以通过阿里云的开源前端网页托管来上传我们的小游戏文件!

我们要登陆uniCloud平台,下面是我们登录后并开通前端页面托管后看到的页面

我们需要把我们的小游戏的根目录文件夹上传到里面

然后点击参数配置,我们可以看到一个默认域名:

我们只需要在默认域名后面加上我们上传的游戏路径就ok了:如下

下面是我的小游戏连接,大家可以看一下下哦~

Cocos Creator | 2048

标签: 算法 游戏引擎

本文转载自: https://blog.csdn.net/lbcyllqj/article/details/127560768
版权归原作者 lqj_本人 所有, 如有侵权,请联系我们删除。

“基于js原生算法+cocos游戏引擎+uni框架Cloud托管网页:开发2048小游戏域名发布版本”的评论:

还没有评论