1,游戏开始前需要检测是否初始生成的时候又三个同块儿,有就将其中一个块儿的颜色调换掉直到没有三个块儿在游戏开始。
continuity:function(){ var sameArr = this.check3Same(); if(sameArr.length>0){ for(var index in sameArr){ var op = sameArr[index]; this.spArr[op.x][op.y].change() } this.continuity() } },
change:function(){ this.type = parseInt(1+Math.random()*4); this.initWithFile("res/pic_"+this.type+".png"); },
2.点击交换换后检测是否有三个连块儿,如果有就进行消除,如果没有就将上次点击交换的两个块儿再换回去。
eliminate:function(arrayIndexArr,cell) { this.logic = true; //------------------ //特殊道具起作用 //检查 arrayIndexArr 里所有的块是否有 special的 var specialArr = [] for(var index in arrayIndexArr){ var arrayIndex = arrayIndexArr[index] if(this.spArr[arrayIndex.x][arrayIndex.y].isSpecial){ for(var i=0;i<GAMEDATA.LINE;i++){ specialArr.push(cc.p(i,arrayIndex.y)) } this.spArr[arrayIndex.x][arrayIndex.y].resetSpecial() } } arrayIndexArr = this.filterDelSame(specialArr, arrayIndexArr) //------------------- if(cell) cell.setToSpecial(); //---------------------------------- for (var index in arrayIndexArr) { var arrayIndex = arrayIndexArr[index]; if(this.spArr[arrayIndex.x][arrayIndex.y] == cell){ continue } cc.eventManager.dispatchCustomEvent(GAMEDATA.JIAFEN,arrayIndexArr.length); if(Math.random()>0.5){ var move = cc.moveTo(1,cc.p(-cc.winSize.width*(1+Math.random()*2), cc.winSize.height*(1+Math.random()*2))); }else { var move = cc.moveTo(1,cc.p(cc.winSize.width*(1+Math.random()*2), cc.winSize.height*(1+Math.random()*2))); } this.spArr[arrayIndex.x][arrayIndex.y].runAction(cc.sequence(move, cc.callFunc(this.removeBullet, this))); this.spArr[arrayIndex.x][arrayIndex.y] = null } for (var i = 0; i < GAMEDATA.LINE; i++) { for (var j = GAMEDATA.LIST - 1; j >= 0; j--) { if (this.spArr[i][j] == null) { continue } var tempAdd = 0; for (var k = j; k < GAMEDATA.LINE; k++) { if (this.spArr[i][k] == null) tempAdd++ } this.spArr[i][j].fallDownNum = tempAdd } } for (var i = 0; i < GAMEDATA.LINE; i++) { for (var j = GAMEDATA.LIST - 1; j >= 0; j--) { this.updateCellByFallDownNum(this.spArr[i][j]) } } this.scheduleOnce(this.fillEmpty.bind(this), 1) },
3.对于三消游戏的话,最重要的就是简化查询过程,特备是死局判定,死局判定的逻辑为,遍历所有的块儿,左右交换,上下交换,例如[1][1]需要和[1][2]还有[2][1]交换并且遍历全局是否有三个同色块儿能够交换,如果有那么返回一个信息,游戏继续,如果没有进行下一个块儿的交换然后在遍历数组,如果这个数组全部交换完成的话,那么就返回游戏进入死局。(其实已经将之前的逻辑在写了一遍)
fillEmpty:function(){ for(var i=0;i<GAMEDATA.LINE;i++){ for(var j=0;j<GAMEDATA.LIST;j++){ if(this.spArr[i][j] == null){ var cell1 = new cell(); cell1.labels(i,j); var move = cc.moveTo(0.5,cc.p(48+i*60,cc.winSize.height-140-j*60)); cell1.runAction(move); //cell1.setPosition(48+i*60,cc.winSize.height-140-j*60); this.addChild(cell1); this.spArr[i][j] = cell1 } } } var end = this.check3Same(); if(end.length == 0){ this.Failure = new FailureDetection(); this.Failure.detection(this.spArr); this.logic = false }else{ this.scheduleOnce(function(){ this.eliminate(end) },0.51) } },---------------------------------------------------------------------------
var FailureDetection = cc.Class.extend({ chanArr:null, detection:function(arr) { this.chanArr = Creat2X2Arr(GAMEDATA.LINE, GAMEDATA.LIST, 1); for (var i = 0; i < GAMEDATA.LINE; i++) { for (var j = 0; j < GAMEDATA.LIST; j++) { this.chanArr[i][j] = arr[i][j].type } } this.check2Same() }, check2Same:function(){ for(var k = 0 ; k < GAMEDATA.LINE-1; k++) { for (var l = 0; l < GAMEDATA.LIST; l++) { var temp1 = this.chanArr[k][l]; this.chanArr[k][l] = this.chanArr[k+1][l]; this.chanArr[k+1][l] = temp1; var end1 = this.check3Same(); if(end1 ==false){ return } this.chanArr[k+1][l] = this.chanArr[k][l]; this.chanArr[k][l] = temp1 } } for(var i = 0 ; i < GAMEDATA.LINE ; i++) { for (var j = 0; j <GAMEDATA.LIST-1; j++) { var temp = this.chanArr[i][j]; this.chanArr[i][j] = this.chanArr[i][j+1]; this.chanArr[i][j+1] = temp; var end = this.check3Same(); if(end ==false){ return } this.chanArr[i][j+1] = this.chanArr[i][j] this.chanArr[i][j] = temp } } cc.log("游戏失败"); return }, check3Same : function(){ for(var i = 0;i < GAMEDATA.LINE;i++){ for(var j = 0;j < GAMEDATA.LINE;j++){ if( i < GAMEDATA.LINE - 2 && this.chanArr[i][j] == this.chanArr[i+1][j] && this.chanArr[i+1][j] == this.chanArr[i+2][j] ){ return false } if( j < GAMEDATA.LINE - 2 && this.chanArr[i][j] == this.chanArr[i][j+1] && this.chanArr[i][j+1] == this.chanArr[i][j+2] ){ return false } } } return true }, });