Touches do not get detected on rare occassions

Can you send me more code, please? Also do you have multi-touch turned on?

Please send me your onTouch Code. and what you are attaching this listener to.

What cocos2d-x version?

What OS are these missing touches being reported on?

Hi,
My game is under testing phase and two testers have reported that touch would not work on gameplay area. They only found this ‘bug’ once and could not re-create it again. Internal testers could not find this issue as well in spite of playing the game since months.

What could cause touches to not respond ?

It would also be helpful if anyone from their own experience could suggest ways or best practices to deal with such issues.

I am registering touch events using the following code

//Inputs
auto touchListener = EventListenerTouchOneByOne::create();
//touchListener->setSwallowTouches(true);
touchListener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
touchListener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);
touchListener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);

_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);

Thanks.

OK, I see. I might attack this a bit different. Consider something like this:

// touch events.
    auto listener = cocos2d::EventListenerTouchOneByOne::create();
    listener->setSwallowTouches(true);
    
    listener->onTouchBegan = [&](cocos2d::Touch* touch, cocos2d::Event* event)
    {
        if (!_gameObject->Instance()->getGameIsPaused())
        {
            cocos2d::Vec2 p = touch->getLocation();
            cocos2d::Rect rect = this->getBoundingBox();
            
            if(rect.containsPoint(p))
            {
                CornSprite::touchEvent(touch);
                return true;
            }
        }
        
        return false;
    };
    
    listener->onTouchEnded = [=](cocos2d::Touch* touch, cocos2d::Event* event)
    {
    };
    
    cocos2d::Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);

I use this code on each Sprite that needs a touch event.

If you are using v3.16 I’d say put the touch events on your items like Sprites, Layers, etc versus one touch event that makes decisions on what was touched. This way you can also set touch priority.

I have not added any code to disable multi touch. Is it on by default ?

Touch listeners are attached to _eventDispatcher which is member variable of CCNode.

Although I cannot reveal the entire code but I can send a basic code structure with pseudo-code.

void HelloWorld::onTouchMoved(Touch* touch, Event* event)
{

}

bool HelloWorld::onTouchBegan(Touch* touch, Event* event)
{
    Vec2 touchLoc = touch->getLocation();
    m_vecLastTouchPos = touchLoc;
    m_vecTouchStartPos = touchLoc;
 
    return true;
}

void HelloWorld::onTouchEnded(Touch* touch, Event* event)
{
    Vec2 touchLoc = touch->getLocation();
	Vec2 touchLocCamera = m_pCamera->convertToNodeSpace(touchLoc);

	if (m_pCamera == NULL)
	{
		return;
	}

    if (m_bTutorialOn)
    {
        //Tutorial related code
    }
    else
    {
        //Check if touches are allowed

        Vec2 touchLocCamera = m_pCamera->convertToNodeSpace(touchLoc);
        
		//Check if character is clicked 
		
		//Check if interactive objects are clicked
		
		//Check if HUD is clicked
        
    }
    
    
    return;
}

In ‘Check if touches are allowed’ part, I just ignore swipe gestures (where start and end points of touch differ by a certain degree).

I am using cocos2d-x 3.16 and this issue was reported on IOS devices only.