小游戏开发 之 消灭星星(cocos-creator)
最近开始搞小游戏,消灭星星太经典了,非常适合拿来练手,核心流程就是《创建方块》、《消除》、《移动》、《结算》。这里只挑重要的讲一下。
方块消除
消除是在发生触摸的时候,只需监听 “touchend” 就好,根据触摸信息得到点击的方块的行和列,之后通过递归得到此次可消除的全部方块,代码如下:
retrieval(row, col){
if(row < 0 || row >= this.rowNum || col < 0 || col >= this.colNum) return;//越界
const cubeNode = this.allCube[row][col];
if(cubeNode == null) return;//为空
var com = cubeNode.getComponent('wss_ps_Cube');
if(com.alreadyRet) return;//已遍历过的位置
com.alreadyRet = true;//初次遍历到,也只会遍历一次
if(com.cubeType!=this.curType){
return;//颜色不同
}
else{
com.willRemove = true;//即将消除
this.removeCount++;//消除数量加1
//遍历四周
this.retrieval(row, col - 1);
this.retrieval(row, col + 1);
this.retrieval(row + 1, col);
this.retrieval(row - 1, col);
}
},
方块移动
移动分为垂直移动和水平移动,如果当次消除导致有空列产生,那么会发生水平方向上的移动,否则只会进行垂直方向的方块移动,代码如下:
//垂直降落
moveDown(){