消除游戏的核心算法

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 ; i<GAME_CONIG.SHRED_NUM_W;i++){
        this.all_arr = arr.slice();                     
        for(var index in arr){                          //遍历四周的块去重复检测它们的四周块
            var po = arr[index];
            this.all_arr = this.all_arr.concat(this._checkNeighbor(po))
        }
        arr = this.all_arr;                            //然后让下一次检测的块给检测数组
    }
    this._updateCellByArr(this.all_arr);               //消除符合情况的块
    this.all_arr = [];                      
},
3.核心:检测四周的块
_checkNeighbor:function(arr){
    var returnArr = [];
    //var dataType = this.shred_arr[data.x][data.y].type;
    var checkArr = [cc.p(-1,0),cc.p(1,0),cc.p(0,-1),cc.p(0,1)];
    for(var index in checkArr){
        var arrIndex = checkArr[index];
        var add = cc.pAdd(arr,arrIndex);                //获取到当前点击对象四周的块
        if(this._checkColor(add,arr)){                  //然后进行检测如果满足要求就把这些块的坐标都存入一个数组里面
            //cc.log("same color",arrIndex.x,arrIndex.y);
            returnArr.push(add);
        }
    }
    return returnArr
},
4.检测是否满足要求
_checkColor:function(arr,targetArr){
    if(arr.x<0||arr.x>GAME_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<GAME_CONIG.SHRED_NUM_W; i++) {
        for (var j = GAME_CONIG.SHRED_NUM_H - 1; j >= 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;*/
                }
            }
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值