【cocos creator】拖拽类,拼图

移动节点,吸附

// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        type: 1,
        ischeckTurn: false,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.node.on(cc.Node.EventType.TOUCH_START, this.touchStartEvent, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMoveEvent, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.touchEndEvent, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchCancel, this);
    },

    start() {
        //this.direction = 1;
    },

    touchStartEvent(event) {
        //点击旋转
        if (this.ischeckTurn) {
            this.node.rotation += 90;
        }
        this._zIndex = this.node.zIndex;
        this._originPos = this.node.getPosition();
        this._startPos = event.getLocation();
        this.node.zIndex += 100;
    },

    touchMoveEvent(event) {
        let pos = event.getLocation();
        if (!this._startPos) {
            return;
        }
        let offset_x = pos.x - this._startPos.x;
        let offset_y = pos.y - this._startPos.y;

        this.node.x = this._originPos.x + offset_x;
        this.node.y = this._originPos.y + offset_y;
    },

    touchEndEvent(event) {
        this._startPos = null;
        this.node.zIndex = this._zIndex;
        cc.log(cc.find("buttonMap"), this.node.parent.parent)
        var Mchildren = cc.find("buttonMap", this.node.parent.parent).children;
        if (this.type === 1) {//吸附格子中央
            var min = {};
            min.length = 99999;
            var pos1 = cc.v2(this.node.x, this.node.y);
            for (var i = 0; i < Mchildren.length; i++) {
                var pos2 = cc.v2(Mchildren[i].x, Mchildren[i].y);
                var temp = pos1.sub(pos2);
                var dis = Math.abs(temp.mag())
                if (min.length > dis) {
                    min.node = Mchildren[i];
                    min.length = dis;
                }
            }
            this.node.x = min.node.x;
            this.node.y = min.node.y;
        }
        else if (this.type === 2) {//吸附格子四边中点
            var min = {};
            min.length = 99999;
            var pos1 = cc.v2(this.node.x, this.node.y);
            for (var i = 0; i < Mchildren.length; i++) {
                var pos2 = cc.v2(Mchildren[i].x + 60, Mchildren[i].y);
                var temp = pos1.sub(pos2);
                var dis = Math.abs(temp.mag())
                if (min.length > dis) {
                    min.node = Mchildren[i];
                    min.length = dis;
                    min.L = true;
                }
                var pos3 = cc.v2(Mchildren[i].x, Mchildren[i].y + 60);
                var temp = pos1.sub(pos3);
                var dis = Math.abs(temp.mag())
                if (min.length > dis) {
                    min.node = Mchildren[i];
                    min.length = dis;
                    min.L = false;
                }
            }
            if (min.L) {
                this.node.x = min.node.x + 60;
                this.node.y = min.node.y;
            }
            else {
                this.node.x = min.node.x;
                this.node.y = min.node.y + 60;
            }
        }
        /*
        this.node.setPosition(GameMgr.GetGridPos(this._row, this._col));
        if (Timer.getTimer() > 0) {
            GameMgr.StartXiaoChu();
        }*/
    },

    touchCancel() {
        this.node.destroy();
    },

    update(dt) {
    },
});


你可能感兴趣的:(cocos)