三消游戏中随机排列图片位置,和点击图片消失下落

var GameData ={
    W_NUM:8,
    H_NUM:8,
}


var HelloWorldLayer = cc.Layer.extend({                                                        
    jiehsou: null,
   sp:null,
    arr:null,
    ctor:function(){
        this._super();
        this._init()
        var that = this

        cc.eventManager.addCustomListener("diaoluo",function(event){                 从精灵类中抛出一个自定义事件,传                                                   出他的arrIndex(点击的图标的下标)                                             消失的图片. 将下标传入一个

                                                                                                                                                       下落方法 

            that.jiehsou = event.getUserData()
            cc.log("xiabiaio", that.jiehsou.x,that.jiehsou.y)
            var i = that.jiehsou.x
            for(var j = that.jiehsou.y - 1; j >= 0; j--){
                if(that.arr[i][j] == null)
                continue;                                                                                    
                that.arr[i][j].fallDown()                                                        
                var a = that.arr[i][j].arrIndex
                that.arr[a.x][a.y] = that.arr[i][j]
                that.arr[i][j] = null
            }


        })


    },
    _init:function(){
        this.arr = []
        for(var i =0 ;i<GameData.W_NUM;i++){
          this.arr [i] = []
            for(var j =0 ;j<GameData.H_NUM;j++){
                this.sp = new Cell(i,j)
                this.sp.setPosition(200+(40*i),cc.winSize.height-20-(40*j))
                this.addChild(this.sp)
                this.sp.setArrIndex(i,j)
                this.arr[i][j] = this.sp
            }
        }
},


});


var HelloWorldScene = cc.Scene.extend({
    onEnter:function () {
        this._super();
        var layer = new HelloWorldLayer();
        this.addChild(layer);
    }

});












Cell= cc.Sprite.extend({
    type:0,
    listener1:null,
    i: null,
    j: null,
    arrIndex: null,
    labei: null,
    ctor:function(i , j) {
        this._super();
        this.j = j;
        this._init()
        this._lear()
        this.scheduleUpdate()
        this.arrIndex = {
            x: i,
            y: j,
        }


    },
    update: function (){
        this.labei.setString(this.arrIndex.x+","+this.arrIndex.y,"",10)
    },

//随机生成图片

    _init:function(){
        this.type =parseInt(1+Math.random()*4);
        this.initWithFile("res/pic"+this.type+".png")
    },
    setArrIndex:function(i,j){                 //设置下标
        this.labei =new cc.LabelTTF(i+","+j,"",10)
        this.labei.enableStroke(cc.color(0,0,0,255),1)
        this.labei.setPosition(10,10)
        this.addChild(this.labei)
    },

// 下落方法
    fallDown: function(){
        var move = cc.moveBy(2, 0, -40)
        this.runAction(move)
        this.arrIndex.y++
    },

//点击事件
    _lear:function(){
        var that = this;
         this.listener1 = cc.EventListener.create({
            event: cc.EventListener.TOUCH_ONE_BY_ONE,
            swallowTouches: true,
            onTouchBegan: function (touch, event) {


                var target = event.getCurrentTarget()
                var locationInNode = target.convertToNodeSpace(touch.getLocation());
                var s = target.getContentSize()
                    var rect = cc.rect(0, 0, s.width, s.height)
                    if (cc.rectContainsPoint(rect, locationInNode) ) {
                        that.removeFromParent(true)  //消除点击的块
                        cc.eventManager.dispatchCustomEvent("diaoluo", that.arrIndex)
                    }


            },
            onTouchMoved: function (touch, event) {
            },
            onTouchEnded: function (touch, event) {
            }
        })
        cc.eventManager.addListener(this.listener1, this)
    }




})


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值