cocos2d-js 屏幕拖拽

原创 2016年09月30日 11:34:45
        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;
        }
    },



版权声明:本文为博主原创文章,未经博主允许不得转载。

IOS 10.3跳转app撰写评论页

10.3之后支持了在app内可以弹出撰写评论页 10.3之前依旧采用openurl方式 #import -(void)requestReview:(NSString*)appID { if...

cocos2d 3.2版本IOS因键盘引起横竖屏问题

在cocos2d 3.2版本中,在IOS8.0以上游戏才用横竖屏都支持后,弹出输入法,或者游戏切到后台弹出输入法再切回来会引起屏幕跑偏,修复方案如下: 修改CCEAGLView.mm文件,注意第一个...

Cocos2d实现类似Clash of clans的缩放拖拽效果

最近开始跳入做游戏的坑了,而且想做一款独立游戏,虽然随时有烂尾的危险,但无妨一试~敬请期待~ 目前正在准备游戏的原型,由于需要设计像COC的缩放场景,作为cocos2d的菜鸟兼ios开发新手,实在s...

(译) Cocos2d_for_iPhone_1_Game_Development_Cookbook:2.1点击、按住、拖拽

(译)Cocos2d_for_iPhone_1_Game_Development_Cookbook 著作权声明:本文由iam126 翻译,欢迎转载分享。 请尊重作者劳动,转载时保留该声 明...
  • iam126
  • iam126
  • 2012年04月04日 19:30
  • 1275

Cocos2D -- 添加点击侦听listener(基本格式、判断点击区域、拖拽跟随移动)

var that = this var listener = cc.EventListener.create({ event: cc.EventListener.TOUCH_ONE_BY_ONE, /...

cocos2d-js 系列[1] 屏幕的适配

屏幕的适配 与Cocos2d-js第一次亲密的接触!

JavaScript强化教程 —— Cocos2d-JS的屏幕适配方案

本文为 H5EDU 机构官方 HTML5培训 教程,主要介绍:JavaScript强化教程​ —— Cocos2d-JS的屏幕适配方案   1. 设置屏幕适配策略(Resolution Pol...

Cocos2d-JS屏幕适配

有终端开发经验的读者应该对屏幕适配问题深感痛恶,因为这个世界实在有太多类型的手机和平板了,各种尺寸首先,我们来认识三个术语: WinSize:设计尺寸,我们设计游戏时预定的目标尺寸,可以在工程中的ma...

Cocos2d-js的屏幕适配方案

Cocos2d-JS中的屏幕适配模式在Web引擎中和原生引擎中的差异比较大,但是API还是统一一致的。本章主要内容以Web引擎中的屏幕适配策略为核心来讨论,原生引擎中行为的不一致性将在本章最后详细介绍...

cocos2d-js 3.0 屏幕适配方案 分辨率适应

cocos2d-js 3.0 屏幕适配方案 分辨率适应 首先介绍一个api和相应的参数: cc.view.setDesignResolutionSize(1024, 768, cc.R...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:cocos2d-js 屏幕拖拽
举报原因:
原因补充:

(最多只允许输入30个字)