cocos2d-x3.10简单手势识别(圆圈,横切,竖切,V型)

手势主要就是判断触摸屏幕时候的坐标来判断,通过坐标和点击的移动的坐标的相对位置来判断手势。记录开始点击的时候的方法,然后记录移动的轨迹,判断形状。

如果是cocos2d-x 2.x的版本的话,点击事项是这样

virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent);
virtual void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent);
virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent);

现在我说的是cocos2d-x 3.x的方法,点击响应不太一样

.h文件

//手势方向
    enum GDirection
    {
        kGDirectionUp = 0,
        kGDirectionDown,
        kGDirectionLeft,
        kGDirectionRight,
        kGDirectionNo,
        kGDirectionCircle,
        kGDirectionV
    };
    
    bool b_click;                   //判断当前是否是单击;
    bool b_debug;                   //调试用;
    bool b_circle;                  //圆;
    bool b_V;                       //V
    bool cache_directionshape[4];   //方向缓存,move中用它来判断是否是单向手势
    GDirection gd_direction;        //手势方向;
    Point ccp_last,ccp_now;       //记录起始、当前坐标

.cpp文件

 //各种手势
    auto listener = EventListenerTouchOneByOne::create();
    listener->onTouchBegan = [&](Touch* touch,Event*event){
        CCLOG("begin");
        ccp_last = Director::getInstance()->convertToGL(touch->getLocationInView());
        b_click = true;
        b_circle = false;
        b_V = false;
        gd_direction = kGDirectionNo;
        for (int i=0; i<4; i++) {
            cache_directionshape[i] = false;
        }
        return true;
    };
    
    listener->onTouchMoved = [&](Touch* touch,Event*event){
         CCLOG("move");
        b_click = false;
        ccp_now = Director::getInstance()->convertToGL(touch->getLocationInView());
        //添加轨迹
        Sprite *sprite = Sprite::create("black.png");
        sprite->setPosition(ccp_now);
        this->addChild(sprite);
        auto func =[=](){
            sprite->removeFromParent();
        };
        CallFunc *callFun = CallFunc::create(func);
        Sequence *seq = Sequence::create(CCDelayTime::create(1),callFun, NULL);
        sprite->runAction(seq);
        //
        float adsx = ccp_now.x - ccp_last.x;
        float adsy = ccp_now.y - ccp_last.y;
        if (std::abs(adsx)>std::abs(adsy)) {
            if (adsx<0) {   //从右往左移
                CCLOG("从右往左移");
                cache_directionshape[0] = 1;
            }
            else{   //从左往右
                 CCLOG("从左往右");
                cache_directionshape[1] = 1;
            }
        }
        else
        {
            if(adsy < 0) {  //从上往下移
                CCLOG("从上往下移");
                
                cache_directionshape[2] = 1;
            }
            else{   //从下往上移
                CCLOG("从下往上移");
                cache_directionshape[3] = 1;
            }
        }
        //画圆
        int x = 0;
        for (int i = 0 ; i< 4 ; i++)
        {
            if(cache_directionshape[i])
                x++;
        }
        if(x >= 3){
            CCLOG("圈");
            b_circle = true;
        }
        if ((x==2)&&(cache_directionshape[2]==1)&&(cache_directionshape[1]==1||cache_directionshape[0]==1)) {
            CCLOG("V");
            b_V = true;
        }
       
    };
    
    listener->onTouchEnded = [&](Touch* touch,Event*event){
        CCLOG("end");
        //圆形;
        if(b_click) {
            return;
        }
        float adsx = ccp_now.x - ccp_last.x;
        float adsy = ccp_now.y - ccp_last.y;
        if(std::abs(adsx) > std::abs(adsy))   //X方向增量大
        {
            if(adsx < 0){
                gd_direction = kGDirectionLeft;
            }
            else{
                gd_direction = kGDirectionRight;
            }
        }
        else
        {
            if(adsy < 0){
                gd_direction = kGDirectionDown;
            }
            else{
                gd_direction = kGDirectionUp;
            }
        }
        if (b_circle) {
            gd_direction = kGDirectionCircle;
        }
        else if(b_V) {
            gd_direction = kGDirectionV;
        }
        cleanDike();
    };
    
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);

这个方法要感谢萝卜_排骨同学的文章,后面会根据这个手势写一个demo小游戏示例。


参考文章:

Cocos2d-X手势之简单实现(方向、模糊圆形处理)

Last modification:January 1st, 1970 at 08:00 am
如果看了这个文章可以让你少加会班,可以请我喝杯可乐
已打赏名单
微信公众号

Leave a Comment