cocos2d-js 屏幕拖拽

cocos2d-js 屏幕拖拽,第1张

概述var tmp = this; cc.eventManager.addListener({ event: cc.EventListener.TOUCH_ONE_BY_ONE, swallowTouches: true, onTouchMoved: function (touches, event) {
        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;        }    },
总结

以上是内存溢出为你收集整理的cocos2d-js 屏幕拖拽全部内容,希望文章能够帮你解决cocos2d-js 屏幕拖拽所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1081983.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存