3.0.0b Touch Listener with dispatch mode ALL_AT_ONCE


I am trying to implement multitouches. I have the touch listener set up in the init() of the layer class as follows

setTouchEnabled(true); //but this function has been declared deprecated anyway
auto dispatcher = Director::getInstance()->getEventDispatcher();
auto touchListener = EventListenerTouchAllAtOnce::create();
touchListener->onTouchesBegan = CC_CALLBACK_2(TerrainLayer::onTouchesBegan, this);
dispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);

And the following is the touch function

void TerrainLayer::onTouchesBegan(const std::vector(Touch *) &touches, cocos2d::Event *unused_event)
    log("num of touches:%d",(int)touches.size());
    	Point location = touches[0]->getLocation();
    	lastTouchPos = location;
    else if(touches.size()==2)
    	log("PINCH BEGIN");

It seems like I unable to get more than a single touch detected. touches.size() always returns 1. Am I missing something here?


Same question. touches.size() always return 1. How to detect for example 3 touches?


you are using 3.0.0b?

I would surely upgrade.


I’m using ver. 3.4 : / But still when I’m touching with 3 fingers i get size = 1 :frowning:
This is my listener:

    auto listener = EventListenerTouchAllAtOnce::create();
    listener->onTouchesBegan = CC_CALLBACK_2(MainScene::onTouchesBegan, this);
    listener->onTouchesMoved = CC_CALLBACK_2(MainScene::onTouchesMoved, this);
    listener->onTouchesEnded = CC_CALLBACK_2(MainScene::onTouchesEnded, this);
    listener->onTouchesCancelled = CC_CALLBACK_2(MainScene::onTouchesCancelled, this);
    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this); 

And in onTouchesBegan:

CCLOG("TOUCH SIZE: %lu", touches.size());

Always 1 :frowning:


Bump! Anyone? :open_mouth:


Device or simulator?


Device :confused: iPad to be more precisely but probably on other devices also .


I’ve the same code like you in 3.6, but I save the touches in myclass for using in other methods, in this way:

In header:

  Vector<Touch *> _touches;

In cpp:

void Myclass::onTouchesBegan(const std::vector<Touch *> &touches, Event *event) {
  for (auto &touch: touches) {

and then I use:

  if (_touches.size() == 1)  { .......
  } else if (_touches.size() >= 2) {   ......... }

The listener creation is like yours


Wait… wont it store all the touches from the WHOLE game… like in the end wont _touches contain like 1000+ touches? I want something that returns 5 when I touch with all my fingers, or 3 when I touch with 3 fingers at one time. In my opinion Your method does not do that… or I am missing something :confused:


Do you have this line in AppController.mm ?

[eaglView setMultipleTouchEnabled:YES];


@Lem, I clear the _touches in OnExit, but I think that piotrros is right, on iOS have you enabled the multitouch?


Yep, that was it… Thanks @piotrros :smiley: I will check now android devices but there multi touches should be by default enabled


On Android you don’t need to enable anything.