如何在cocos2d-X3.2中禁用Android中的多点触控
我正在以这种方式设置单触处理程序如何在cocos2d-X3.2中禁用Android中的多点触控,cocos2d-x,cocos2d-x-3.0,Cocos2d X,Cocos2d X 3.0,我正在以这种方式设置单触处理程序 auto touchListener = EventListenerTouchOneByOne::create(); touchListener->setSwallowTouches(true); touchListener->onTouchBegan = CC_CALLBACK_2(MyClass::onTouchBegan, this); touchListener->onTouchMoved = CC_CALLBACK_2(MyClas
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来说,如果我同时用两个手指触摸屏幕,它将调用OnTouch两次
如何从Android的cocos2d-x(3.2)代码中禁用多点触控?我找到了一个解决方法,因为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
}
bool MyClass::ontouch开始(cocos2d::Touch*Touch,cocos2d::Event*Event)
{
如果(_currentTouchID<0){
_currentTouchID=touch->getID();
}
否则{
返回false;
}
//你的代码在这里
返回true;
}
void MyClass::onTouchMoved(cocos2d::Touch*Touch,cocos2d::Event*Event)
{
如果(\u currentTouchID!=touch->getID()){
返回;
}
//你的代码在这里
}
void MyClass::OnTouched(cocos2d::Touch*Touch,cocos2d::Event*Event)
{
如果(_currentTouchID==touch->getID()){
_currentTouchID=-1;
}
否则{
返回;
}
//你的代码在这里
}
就这样。如果您找到了更好的解决方案,请提供您的解决方案
顺便说一句:在Cocos2dxGLSurfaceView.java文件中,注释switch case MotionEvent.ACTION_POINTER_DOWN:,
因为它是在cocos2d-x论坛上提供的,对我来说不起作用