Swallow touches EventListenerMouse

Swallow touches EventListenerMouse
0.0 0


Hello World!
When i figure out ( inside some of EventListenerMouse callbacks ) that i focus on some Node i need to prevent continue of Mouse event propagation. ( Analogy swallowTouches in EventListenerTouchOneByOne ).
How to implement such behavior with EventListenerMouse?


I can’t check right now, but, will EventMouse::stopPropagation help me?


the same way you do with touch. check to see what you clicked on and if it is what you want return a bool to represent how you want to handle it.

basically this same thing:

auto listener1 = EventListenerTouchOneByOne::create();

// trigger when you push down
listener1->onTouchBegan = [](Touch* touch, Event* event){
    // your code
    return true; // if you are consuming it

// trigger when moving touch
listener1->onTouchMoved = [](Touch* touch, Event* event){
    // your code

// trigger when you let up
listener1->onTouchEnded = [=](Touch* touch, Event* event){
    // your code


According to http://www.cocos2d-x.org/reference/native-cpp/V3.0beta2/d6/d70/classcocos2d_1_1_event_listener_mouse.html
All calbacks has void(Event *event) semantic, and, object of type EventListenerMouse doesn’t have setSwallow… method.


notice this!


Yes, I believe stopPropagation is the way to go for EventListenerMouse.


I’m now in the habit of treating touch and mouse very similar in my handling. What do you do? You are on 2.2.6, right?


I use both 2.x and 3.x, but I have never created a desktop app, i.e. an app that uses a mouse, in 2.x. In 3.x, all EventListenerMouse callbacks are void functions. I tested this and can confirm that returning true or false from onMouseBegan does nothing.


in 2.2.6 and what version of 3 did you test?


I tested this is in 3.14.1. The code for the callbacks in EventListenerMouse for both 3.14.1 and the latest version (3.16) is:

std::function<void(EventMouse* event)> onMouseDown;
std::function<void(EventMouse* event)> onMouseUp;
std::function<void(EventMouse* event)> onMouseMove;
std::function<void(EventMouse* event)> onMouseScroll;


Just investigated and here is what happens when you use the mouse:

  1. The EventMouse is created and is sent to the dispatcher.
  2. The dispatcher gets each listener for mouse events and dispatches the event to each of them.
  3. After a listener has been sent the event, event->isStopped() is used to check if the event should not be sent to other listeners. isStopped is set to true when stopPropagation() is used.


We should update the event dispatch docs to reflect this. I think folks should know about this. I hadn’t looked before.