在代码身上,用心去飞翔

你是我的小啊小code,怎么爱你都不嫌多

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, event);
            },
            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, 0, 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://blog.csdn.net/zhenyu5211314/article/details/52710304
文章标签: js
个人分类: Js
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭