Reputation: 71
I am setting handlers for single touch in this way
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);
For iOS it works properly, but for Android if I touch the screen with two fingers in the same time it will call onTouchBegan twice.
How can I disable multitouch from cocos2d-x (3.2) code for Android?
Upvotes: 3
Views: 1672
Reputation: 71
I found a workaround, since cocos2d-x doesn't have an official solution for this. (using Cocos2d-x 3.2)
Since each touch has it's own ID, we just need to filter first touch ID from others. I have achieved this in the following way:
Created layer's instance variable:
int _currentTouchID;
Initialised it with -1 in layer's init() method:
_currentTouchID = -1;
In the beginign of all touch handlers I did next:
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
}
That's it. Please provide your solution if you have found a better one.
BTW: Commenting the switch case MotionEvent.ACTION_POINTER_DOWN: in Cocos2dxGLSurfaceView.java file, as it was offered on cocos2d-x forum, didn't work for me.
Upvotes: 3