Cocos2dx3. X touch devouring Function

Source: Internet
Author: User
You should be clear about the dialog box. The modal dialog box is a dialog box that does not allow operations on elements other than the current dialog box when the current dialog box appears. Cocos does not contain the built-in dialog box, so you need to make it yourself. Generally, a new layer is created, covering the current scenario. But at this time, there is often a problem, that is, coverage.

You should be clear about the dialog box. The modal dialog box is a dialog box that does not allow operations on elements other than the current dialog box when the current dialog box appears. Cocos does not contain the built-in dialog box, so you need to make it yourself. Generally, a new layer is created, covering the current scenario. But at this time, there is often a problem, that is, coverage.

You should be clear about the dialog box. The modal dialog box is a dialog box that does not allow operations on elements other than the current dialog box when the current dialog box appears.

Cocos does not contain the built-in dialog box, so you need to make it yourself. Generally, a new layer is created, covering the current scenario.

However, there is often a problem at this time, that is, after overwriting, the lower-layer click operation will still be matched.

In my previous practice, I compared naive (of course, it is also a method), that is, every time the control of the lower-layer scenario is uploaded to the upper layer and setEnable (false) is dropped. But... It is too troublesome.


Today, we know that the upper-layer dialog box layer can be used to swallow the touch, that is, all the touch operationsIntercept at current LayerIt is not allowed to respond in the lower layer.

In 3.x, due to changes in the event processing mechanism, you need to implement a listener (listener) to listen to the current layer touch operations andPrevent them from spreading to lower layers.

The Code is as follows (generally written inInit function at the dialog Layer). IfDialog Box LayerAnd the listener must be fixed inThisIf you add listener to the next layer of the dialog boxDialog Box layer instance(That isNot ths) Fixed listener:

auto listener = EventListenerTouchOneByOne::create();listener->setSwallowTouches(true);listener->onTouchBegan = [](Touch* touch, Event* event){return true;};_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);

For more information, see. You can also see here:
  1. SetSwallowTouches is to restrict whether the current layer needs to intercept all touch events. true is to intercept;
  2. OnTouchBegan is generally used to handle touch events. Here, it is written empty, just let it return a value.

    (1) If true is returned, the Touch event of the current layer can be triggered, andBlock backward Transmission(You can use it by yourself.);

    (2) If false is returned, subsequent Touch events at the current layer cannot be triggered and are passed back.

  3. AddEventListenerWithSceneGraphPriority is a function for adding listeners. You can also write addEventListenerWithFixedPriority. The difference is:

    (1) The priority of the addEventListenerWithSceneGraphPriority event listener is 0, and the priority of the event listener in addEventListenerWithFixedPriority cannot be set to 0, because this is reserved for SceneGraphPriority;

    (2) In addition, it is very important,FixedPriorityAfter adding the listenerManually removeThe SceneGraphPriority listener is bound to the node and will be removed from the node destructor.

  4. _ EventDispatcher is a cocos built-in event distributor, which can be directly used without declaration.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.