3.0.0b Touch Listener with dispatch mode ALL_AT_ONCE


#1

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
setTouchMode(Touch::DispatchMode::ALL_AT_ONCE);
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());
    if(touches.size()==1)
    {
    	//Pan
    	Point location = touches[0]->getLocation();
    	lastTouchPos = location;
    }
    else if(touches.size()==2)
    {
    	//Pinch
    	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?


#2

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


#3

you are using 3.0.0b?

I would surely upgrade.


#4

@slackmoehrle
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:


#5

Bump! Anyone? :open_mouth:


#6

Device or simulator?


#7

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


#8

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:

private:
  Vector<Touch *> _touches;

In cpp:

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

and then I use:

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

The listener creation is like yours


#9

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:


#10

Do you have this line in AppController.mm ?

[eaglView setMultipleTouchEnabled:YES];


#11

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


#12

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


#13

On Android you don’t need to enable anything.