您的位置:首页 > 其它

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

2016-07-05 15:10 162 查看
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)

    }

})
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: