消除游戏的核心算法

1.通过自定义事件从点击的对象类里面抛出当前点击的对象的坐标
cc.eventManager.dispatchCustomEvent(USER_CLICK_SHRED_EVERT, that.arrayIndex);
2.然后获取到坐标后进行处理
cc.eventManager.addCustomListener(USER_CLICK_SHRED_EVERT,this._checkArr.bind(this));
_checkArr:function(event){
    var touchIndex = event.getUserData();
    this.all_arr.push(touchIndex);                      //用一个数组去存取当前点击的对象
    var arr = this._checkNeighbor(touchIndex);          //获取当前点击对象的四周                            
    for(var i = 0 ; iGAME_CONIG.SHRED_NUM_H-1||arr.y<0||arr.y>GAME_CONIG.SHRED_NUM_W-1){
        return false;                                   //检查是否越界
    }
    for(var index in this.all_arr){
        var sameArr = this.all_arr[index];               
        if(sameArr.x == arr.x&&sameArr.y == arr.y ) return false         //检查数组中有没有重复的块
     }
    if(this.shred_arr[arr.x][arr.y] == null ||this.shred_arr[targetArr.x][targetArr.y] == null) return false;     
     //检查有没有块是消除下落后不存在的        这里是个坑,如果不写会出现bug
    if(this.shred_arr[arr.x][arr.y].type == this.shred_arr[targetArr.x][targetArr.y].type) return true
},   //检查当前块与周围块颜色是否一致
5.块消除后掉落
_checkDown:function(){
    for(var i = 0; i= 0; j--) {
            var downNum = 0;
            if (this.shred_arr[i][j] != null) {
                for (var k = j; k < GAME_CONIG.SHRED_NUM_H; k++) {
                    if (this.shred_arr[i][k] == null) downNum++
                }
                if (downNum != 0) {
                    this.shred_arr[i][j].fallDown(downNum);                   
                    var moveEndIndex = this.shred_arr[i][j].arrayIndex;   //获取更新后的值
                    this.shred_arr[moveEndIndex.x][moveEndIndex.y] = this.shred_arr[i][j];
                    this.shred_arr[i][j] = null
                    /* this.shred_arr[i][j+downNum]=this.shred_arr[i][j];
                    this.shred_arr[i][j]=null;*/
                }
            }
        }
    }
}


你可能感兴趣的:(js)