cocos creator 判断滑动方向


定义变量

public firstX = null;
public firsty = null;

点击 获取坐标

this.viewNode.on(cc.Node.EventType.TOUCH_START,function(event){
    let location = event.getLocation();// 获取节点坐标
    this.firstX = location.x;
    this.firstY = location.y;
    // 获取触点在空间节点上的坐标
    // var tempPlayer = node.parent.convertToNodeSpaceAR(location);
    // node.setPosition(tempPlayer);
},this);

抬起后判断滑动方向

this.viewNode.on(cc.Node.EventType.TOUCH_END,function(event){
    let touchPoint = event.getLocation();
    let endX = this.firstX - touchPoint.x;
    let endY = this.firstY - touchPoint.y;
    // var tempPlayer = node.parent.convertToNodeSpaceAR(touchPoint);
    // node.setPosition(tempPlayer);

    if (Math.abs(endX) > Math.abs(endY)){
        //手势向左右
        //判断向左还是向右 
        if (endX  > 0){
            //向左函数
            console.log('left');
        } else {
            //向右函数
            console.log('right');
        }
    } else {
        //手势向上下
        //判断手势向上还是向下
        if (endY  > 0){
            //向下函数
            console.log('down');
        } else {
            //向上函数
            console.log('up');
        }
     }
},this);

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM