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