【问题标题】:How can I use swipe gestures in cocos2d-js?如何在 cocos2d-js 中使用滑动手势?
【发布时间】:2014-05-23 05:19:48
【问题描述】:

我正在研究如何在 cocos2d-js 中使用滑动手势,发现在 cocos2d 中使用了 UISwipeGestureRecognizer。但是 cocos2d-js 找不到。

Gestures in cocos2d

还有 github 中的 cocos2d-x:

CCGestureRecognizer

对于 cocos2d-js 我只找到了

        cc.eventManager.addListener({
            event: cc.EventListener.TOUCH_ALL_AT_ONCE,
            onTouchesMoved:function (touches, event) {
                event.getCurrentTarget().processEvent(touches[0]);
            }
        }, this);

带有额外的事件类型:

onTouchesBegan
onTouchesEnded
onTouchesCancelled

这就是 cocos2d-js 中检测左右滑动的所有帮助吗?

【问题讨论】:

    标签: javascript cocos2d-js


    【解决方案1】:

    这是我的解决方案,针对 cocos2d-js 3.0a2 进行了测试:

       if( true || 'touches' in cc.sys.capabilities ) { // touches work on mac but return false
            cc.eventManager.addListener(cc.EventListener.create({
                event: cc.EventListener.TOUCH_ALL_AT_ONCE,
                onTouchesBegan: function(touches, event) {
                    console.log("onTouchesBegan!");
    
                    var touch = touches[0];
                    var loc = touch.getLocation();
    
                    self.touchStartPoint = {
                        x: loc.x,
                        y: loc.y
                    };
    
                    self.touchLastPoint = {
                            x: loc.x,
                            y: loc.y
                    };
                },
    
                onTouchesMoved: function(touches, event) {
                    var touch = touches[0];
                    var loc = touch.getLocation(),
                        start = self.touchStartPoint;
    
                    // check for left
                    if( loc.x < start.x - self.touchThreshold ) {
                        // if direction changed while swiping left, set new base point
                        if( loc.x > self.touchLastPoint.x ) {
                            start = self.touchStartPoint = {
                                    x: loc.x,
                                    y: loc.y
                            };
                            self.isSwipeLeft = false;
                        } else {
                            self.isSwipeLeft = true;                        
                        }
                    }
    
                    // check for right
                    if( loc.x > start.x + self.touchThreshold ) {
                        // if direction changed while swiping right, set new base point
                        if( loc.x < self.touchLastPoint.x ) {
                            self.touchStartPoint = {
                                    x: loc.x,
                                    y: loc.y
                            };
                            self.isSwipeRight = false;
                        } else {
                            self.isSwipeRight = true;                       
                        }
                    }
    
                    // check for down
                    if( loc.y < start.y - self.touchThreshold ) {
                        // if direction changed while swiping down, set new base point
                        if( loc.y > self.touchLastPoint.y ) {
                            self.touchStartPoint = {
                                    x: loc.x,
                                    y: loc.y
                            };
                            self.isSwipeDown = false;
                        } else {
                            self.isSwipeDown = true;                        
                        }
                    }
    
                    // check for up
                    if( loc.y > start.y + self.touchThreshold ) {
                        // if direction changed while swiping right, set new base point
                        if( loc.y < self.touchLastPoint.y ) {
                            self.touchStartPoint = {
                                    x: loc.x,
                                    y: loc.y
                            };
                            self.isSwipeUp = false;
                        } else {
                            self.isSwipeUp = true;                      
                        }
                    }
    
                    self.touchLastPoint = {
                            x: loc.x,
                            y: loc.y
                    };
                },
    
                onTouchesEnded: function(touches, event){
                    console.log("onTouchesEnded!");
    
                    var touch = touches[0],
                        loc = touch.getLocation()
                        size = self.size;
    
                    self.touchStartPoint = null;
    
                    if( !self.isSwipeUp && !self.isSwipeLeft && !self.isSwipeRight && !self.isSwipeDown ) {
                        if( loc.y > size.height*0.25 && loc.y < size.height*0.75 ) {
                            (loc.x < size.width*0.50)? self.isTouchLeft = true : self.isTouchRight = true;
                        } else if( loc.y > size.height*0.75 ) {
                            self.isTouchUp = true;
                        } else {
                            self.isTouchDown = true;
                        }
                    }
    
                    self.isSwipeUp = self.isSwipeLeft = self.isSwipeRight = self.isSwipeDown = false;
    
                    //location.y = self.size.height;
                    //event.getCurrentTarget().addNewTileWithCoords(location);
                }
            }), this);
        } else {
            cc.log("TOUCH_ALL_AT_ONCE is not supported");
        }
    

    【讨论】:

    • this.touchThreshold 未定义。你能告诉我,this.touchThreshold 是什么吗?
    • @Sudhakar 那是一个像素值。我想,我用了10个。没有把握。但无论如何,这取决于您的需求。尝试不同的...
    【解决方案2】:

    这是我在 cocos2d-js 刷卡的解决方案

    var cambaglayer = cc.Layer.extend({
                                  ctor:function () {
    
    
    
    
                                  this._super();
                                  var size = cc.winSize;
    
                                  touchCounter = 0;
                                  if( true || 'touches' in cc.sys.capabilities ) {
    
    
                                  cc.eventManager.addListener(cc.EventListener.create({
                 event: cc.EventListener.TOUCH_ALL_AT_ONCE,
                 onTouchesBegan: function(touches, event) {
                 console.log("onTouchesBegan!");
                 touchMenu();
                 var touch = touches[0];
                 var loc = touch.getLocation();
                 this.touchStartPoint = {
                 x: loc.x,
                 y: loc.y
                 };
                 this.touchLastPoint = {
                 x: loc.x,
                 y: loc.y
                 };
                 },
                 onTouchesMoved: function(touches, event) {
                 var touch = touches[0];
                 var loc = touch.getLocation(),
                 start = this.touchStartPoint;
                 console.log("onTouchesMoved!");
                 console.log("onTouchesMoved!"+ touchThreshold);
                 if( loc.x < start.x - touchThreshold ) {
                 console.log("left!");
                 if( loc.x > this.touchLastPoint.x ) {
                 start = this.touchStartPoint = {
                 x: loc.x,
                 y: loc.y
                 };
                 this.isSwipeLeft = false;
                 } else {
                 this.isSwipeLeft = true;
                 cc.log("swiping left side") 
                 }
                 }
                 if( loc.x > start.x + touchThreshold ) {
                 console.log("right!");
    
                 if( loc.x < this.touchLastPoint.x ) {
                 this.touchStartPoint = {
                 x: loc.x,
                 y: loc.y
                 };
                 this.isSwipeRight = false;
                 } else {
                 this.isSwipeRight = true;
                 console.log("Swiping Right side");
                 }
                 }
                 this.touchLastPoint = {
                 x: loc.x,
                 y: loc.y
                 };
                 },
                 }
                 ), this);
                                  } else {
                                  cc.log("TOUCH_ALL_AT_ONCE is not supported");
                                  }
                                  return true;
                                  }
                                  });
    

    【讨论】:

      【解决方案3】:

      不是一个完整的答案,但是:您可以简单地在onTouchesBegan 上获取触摸的起始位置和在onTouchesEnded 上的结束位置,然后通过减去位置并查看XY 来猜测滑动的方向变化最大(以及在哪个部分)。

      如果您正在寻找比上/下/左/右更强大的手势识别器,我能想到的只是将您推荐给this sample project using dollar gesture recognizer by supersuraccoon,尽管必须注意它是旧代码(v2.2.2? ) 并且 cocos2d-html5 v3 中的事件管理器有点不同,所以它可能需要在你当前的水平之上做一些工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-15
        • 1970-01-01
        相关资源
        最近更新 更多