【问题标题】:How to disable multitouch in Android in cocos2d-x 3.2如何在 cocos2d-x 3.2 中禁用 Android 中的多点触控
【发布时间】:2014-10-08 23:33:18
【问题描述】:

我正在以这种方式为单点触控设置处理程序

auto touchListener = EventListenerTouchOneByOne::create();
touchListener->setSwallowTouches(true);

touchListener->onTouchBegan = CC_CALLBACK_2(MyClass::onTouchBegan, this);
touchListener->onTouchMoved = CC_CALLBACK_2(MyClass::onTouchMoved, this);
touchListener->onTouchEnded = CC_CALLBACK_2(MyClass::onTouchEnded, this);

auto dispatcher = Director::getInstance()->getEventDispatcher();
dispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);

对于 iOS,它可以正常工作,但对于 Android,如果我用两根手指同时触摸屏幕,它会调用两次 onTouchBegan。

如何在 Android 的 cocos2d-x (3.2) 代码中禁用多点触控?

【问题讨论】:

    标签: cocos2d-x cocos2d-x-3.0


    【解决方案1】:

    我找到了一个解决方法,因为 cocos2d-x 没有官方的解决方案。 (使用 Cocos2d-x 3.2)

    由于每个触摸都有自己的 ID,我们只需要从其他触摸中过滤掉第一个触摸 ID。 我通过以下方式实现了这一点:

    创建层的实例变量:

    int _currentTouchID;
    

    在层的 init() 方法中使用 -1 对其进行初始化:

    _currentTouchID = -1;
    

    在我接下来所做的所有触摸处理程序的开始:

    bool MyClass::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event)
    {
        if (_currentTouchID < 0) {
            _currentTouchID = touch->getID();
        }
        else {
            return false;
        }
        //Your code here
    
        return true;
    }
    
    void MyClass::onTouchMoved(cocos2d::Touch *touch, cocos2d::Event *event)
    {
        if (_currentTouchID != touch->getID()) {
            return;
        }
        //Your code here
    }
    
    void MyClass::onTouchEnded(cocos2d::Touch *touch, cocos2d::Event *event)
    {
        if (_currentTouchID == touch->getID()) { 
            _currentTouchID = -1; 
        }
        else { 
            return; 
        }
        //Your code here
    }
    

    就是这样。如果您找到了更好的解决方案,请提供您的解决方案。

    顺便说一句:在 Cocos2dxGLSurfaceView.java 文件中评论开关案例 MotionEvent.ACTION_POINTER_DOWN: 因为它是在 cocos2d-x 论坛上提供的,对我不起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 2012-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多