[linux&android] ccTouchesBegan is not called

[linux&android] ccTouchesBegan is not called
0.0 0


I use cocos2d-x 2.2 and I’m trying to detect touch events.

At first, I was developing in linux in order to test the application without using my mobil. But I noticed that ccTouchesBegan weren’t called. So looking on the Internet I read that mouse clicks are not mapped as touch events. Is that true?

Ok. Then I tried to test my game in my android device. But the event is not called either. So I put an std::cout to send a message, so that I can know if the function is called (maybe the error comes from my code). But the method is not called.

Now I don’t know if Android doesn’t give support to std::cout and thats why I cannot see any message in logcat. Or maybe the problem is that the method is not being called. Do I have to enable something?

I wrote the method as a class method:

HelloWorld::ccTouchesBean() { …cout&something… } in my HelloWorldScene.cpp. Is It this implementation correct?

Thank you in advance!


I have moved to v3 alpha1 but for v2 you need to register with the Touch Dispatcher and decide if you are going to swallow the event.

and you have ‘HelloWorld::ccTouchesBean()’ and it should be ’HelloWorld::ccTouchesBegin()` You need to also implement other functions.

Try this: http://stackoverflow.com/questions/11141112/detect-touch-cocos2d-x


Step 1, in HelloWorldLayer::Init(), write this:


Step 2, override registerWithTouchDispatcher:

void HelloWorldLayer::registerWithTouchDispatcher(void) {
    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, ...);

Step 3, write functions to handle touch event:

virtual bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
virtual void ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
virtual void ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
virtual void ccTouchCancelled(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);


I know this is an old conversation, but my company is still using cocos2d-x v2.
Please correct my understanding if I’m wrong, but must we always add a targeted delegate to enable touch?
Must we not remove that delegate later to prevent a leak?