cocos2d-js 屏幕拖拽

前端之家收集整理的这篇文章主要介绍了cocos2d-js 屏幕拖拽前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
        var tmp = this;
        cc.eventManager.addListener({
            event: cc.EventListener.TOUCH_ONE_BY_ONE,swallowTouches: true,onTouchMoved: function (touches,event) {
                tmp.onTouchesMoved(touches,event);
            },onTouchEnded: function (touches,event) {
                tmp.onTouchesEnded(touches,onTouchBegan: function (touches,event) {
                //tmp.onTouchesBegan(touches,event);
                return true;
            }
        },this);
touchmove的时候定义好边界:
    onTouchesBegan: function (touches,event) {
        // cc.log("onTouchBegan");
    },onTouchesMoved: function (touches,event) {
        //cc.log("onTouchMoved");
        this.onTouchCheckBounder(touches.getDelta(),true);

        var showNewX = false;
        var showNewY = false;
        var offLastX = this._origin_pos.x + this._offset_x - this._last_pos.x;
        if (Math.abs(offLastX) >= this._cellSize.width) {
            showNewX = true;
            var addXNum = Math.floor(Math.abs(offLastX)/this._cellSize.width + 0.5);
            this.moveCellsWithMap(addXNum,offLastX < 0,false);
            if (offLastX < 0) {
                this._offset_x = this._last_pos.x - this._origin_pos.x - this._cellSize.width * addXNum;
            } else {
                this._offset_x = this._last_pos.x - this._origin_pos.x + this._cellSize.width * addXNum;
            }
            var moveDir = offLastX < 0 ? "->>>>>> " : "<<<<<<- ";
            cc.log(moveDir + addXNum);
        }
        var offLastY = this._origin_pos.y + this._offset_y - this._last_pos.y;
        if (Math.abs(offLastY) >= this._cellSize.height) {
            showNewY = true;
            var addYNum = Math.floor(Math.abs(offLastY)/this._cellSize.height + 0.5);
            this.moveCellsWithMap(0,addYNum,false,offLastY < 0);
            if (offLastY < 0) {
                this._offset_y = this._last_pos.y - this._origin_pos.y - this._cellSize.height * addYNum;
            } else {
                this._offset_y = this._last_pos.y - this._origin_pos.y + this._cellSize.height * addYNum;
            }
            var moveDir = offLastY < 0 ? "-^^^^^^ " : "vvvvvv- ";
            cc.log(moveDir + addYNum);
        }

        this._mainLayer.setPosition(cc.p(this._origin_pos.x + this._offset_x,this._origin_pos.y + this._offset_y));
        if (showNewX) this._last_pos.x = this._mainLayer.getPosition().x;
        if (showNewY) this._last_pos.y = this._mainLayer.getPosition().y;
    },// 检查拖拽差量是否接触边界
    onTouchCheckBounder: function(pos_delta,touch) {
        var isBounder = {x:false,y:false};
        var dragDistanceXLeft = this._xNum*this._cellSize.width - g_winSize.width + 46;
        var dragDistanceXRight = 0;
        var dragDistanceYDown = this._yNum*this._cellSize.height - g_winSize.height + this._cellSize.height/2;
        var dragDistanceYUp = 0;
        var pos_x = (this._offset_x + pos_delta.x);
        var pos_y = (this._offset_y + pos_delta.y);
        if (pos_x < -dragDistanceXLeft) {
            if (touch) this._offset_x = -dragDistanceXLeft;
            isBounder.x = true;
        } else if (pos_x > dragDistanceXRight) {
            if (touch) this._offset_x = dragDistanceXRight;
            isBounder.x = true;
        } else {
            if (touch) this._offset_x = pos_x;
        }

        if (pos_y < -dragDistanceYDown) {
            if (touch) this._offset_y = -dragDistanceYDown;
            isBounder.y = true;
        } else if (pos_y > dragDistanceYUp) {
            if (touch) this._offset_y = dragDistanceYUp;
            isBounder.y = true;
        } else {
            if (touch) this._offset_y = pos_y;
        }
        return isBounder;
    },onTouchesEnded: function (touches,event) {
        // cc.log("onTouchEnded");
    },


点击事件影响拖拽处理方法

    touchCellEventBtn: function (sender,type) {
        switch (type) {
            case ccui.Widget.TOUCH_BEGAN:
                this._btn_pos_touch_began = sender.getTouchBeganPosition();
                this._btn_touch_state = true;
                // CocosUtility.ImageEvent_OnTouchBegan(sender);
                break;
            case ccui.Widget.TOUCH_CANCELED:
                this._btn_touch_state = false;
                // CocosUtility.ImageEvent_OnTouchCancelled(sender);
                break;
            case ccui.Widget.TOUCH_MOVED:
                var movedPos = sender.getTouchMovePosition();
                var offset_x = movedPos.x - this._btn_pos_touch_began.x;
                var offset_y = movedPos.y - this._btn_pos_touch_began.y;
                this._btn_touch_state = offset_x < 20 && offset_x > -20 && offset_y < 20 && offset_y > -20;
                //CocosUtility.ImageEvent_OnTouchMoved(sender);
                break;
            case ccui.Widget.TOUCH_ENDED:
                // CocosUtility.ImageEvent_OnTouchEnded(sender);
                if (!this._btn_touch_state) return;
                this._btn_touch_state = false;
                UIAudio.openUI();
                if (sender.getTag() >= 10*this.B_LARGE) {
                    cc.log("click cloud");
                } else if (sender.getTag() >= 5*this.B_LARGE) {
                    this.onSightClicked(sender.getTag()-5*this.B_LARGE,sender._sightType);
                } else if (sender.getTag() >= this.B_LARGE) {
                    this.onBoatClicked(sender.getTag()-this.B_LARGE,sender._userId);
                } else {
                    this.onCellClicked(sender.getTag());
                }
                break;
        }
    },
原文链接:https://www.f2er.com/cocos2dx/339156.html

猜你在找的Cocos2d-x相关文章