转载于: http://t.zoukankan.com/jhy-ocean-p-11436498.html
定义变量
firstX = null; 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);
备注: 绑定方法时候 一定要在最后参数添加 this 否则可能事件不生效
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步