定義變量
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);