Touch mechanism in cocos2dx 3.2

Source: Internet
Author: User

Touch mechanism in cocos2dx 3.2

In cocos2dx 3.2, the previous 2.x version is discarded. Let's take a look at a piece of code in Layer. h.

// Single-Touch virtual bool onTouchBegan (touch * Touch, Event * unused_event); virtual void onTouchMoved (touch * Touch, Event * unused_event); virtual void onTouchEnded (touch * Touch, event * unused_event); virtual void onTouchCancelled (Touch * touch, Event * unused_event); // multi-point Touch virtual void onTouchesBegan (const std: vector
 
  
& Touches, Event * unused_event); virtual void onTouchesMoved (const std: vector
  
   
& Touches, Event * unused_event); virtual void onTouchesEnded (const std: vector
   
    
& Touches, Event * unused_event); virtual void onTouchesCancelled (const std: vector
    
     
& Touches, Event * unused_event );
    
   
  
 

Single touch: (only the registered Layer can receive touch events)

OnTouchBegan: if true is returned, subsequent Touch events at the current layer can be triggered, and the backlayer transmission is blocked.

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

To put it simply, if

1. The Layer has only one Layer:

Virtual bool onTouchBegan (CCTouch * pTouch, CCEvent * pEvent );

A. if false is returned, the ccTouchMoved () and ccTouchEnded () messages will not be received.

B. if true is returned, the ccTouchMoved () and ccTouchEnded () messages can be received.

2. The Layer has multiple layers:

Virtual bool onTouchBegan (CCTouch * pTouch, CCEvent * pEvent );

A. if false is returned, onTouchMoved () and onTouchEnded () at the current layer will no longer receive the message, but other layers under this layer will receive the message.

B. if true is returned, onTouchMoved () and onTouchEnded () at the current layer can receive messages, but other layers at the current layer cannot receive messages.

Simple single touch usage:

Add the following code to the Layer and rewrite the onTouchxxx function.

Auto dispatcher = Director: getInstance ()-> getEventDispatcher (); auto listener = listener: create (); listener-> onTouchBegan = CC_CALLBACK_2 (GameLayer: onTouchBegan, this ); listener-> onTouchMoved = CC_CALLBACK_2 (GameLayer: onTouchMoved, this); listener-> onTouchEnded = CC_CALLBACK_2 (GameLayer: onTouchEnded, this); listener-> true ); // do not pass down the touch dispatcher-> addEventListenerWithSceneGraphPriority (listener, this );

Listener-> setSwallowTouches (true), do not touch down, simply put, for example, there are two sprite, A and B, A on B under (position overlap), when you touch, B will not be affected

Listener-> setSwallowTouches (false); otherwise, the touch is passed down, And the touch A is equal to the touch B.


Multi-point touch point single usage (multiple layers obtain screen events ):

auto dispatcher = Director::getInstance()->getEventDispatcher();  auto listener1 = EventListenerTouchAllAtOnce::create();  listener1->onTouchesBegan = CC_CALLBACK_2(GameLayer::onTouchesBegan,this);  listener1->onTouchesMoved = CC_CALLBACK_2(GameLayer::onTouchesMoved,this);  listener1->onTouchesEnded = CC_CALLBACK_2(GameLayer::onTouchesEnded,this);  dispatcher->addEventListenerWithSceneGraphPriority(listener1,this);
Or setTouchEnabled (true), and then override the onTouchsxxx function of the layer.

About eventDispatcher:

  • Method:

  • auto dispatcher = Director::getInstance()->getEventDispatcher();

    Event Listeners include the following types:

    • EventListenerTouch)
    • EventListenerKeyboard)
    • EventListenerAcceleration)
    • EventListenerMouse)
    • Custom Event (EventListenerCustom)

      All the above event listeners are composed_eventDispatcher.


      Priority:

      1. The lower the priority, the more responsive the event

      2. If the priorities are the same, the upper layer (Z axis) receives touch events first.

      There are two ways to add the event listener listener1 to the event scheduler _ eventDispatcher:

      void EventDispatcher::addEventListenerWithSceneGraphPriority(EventListener* listener, Node* node)    void EventDispatcher::addEventListenerWithFixedPriority(EventListener* listener, int fixedPriority)
      The code is expanded as follows:
      void EventDispatcher::addEventListenerWithSceneGraphPriority(EventListener* listener, Node* node){  CCASSERT(listener && node, "Invalid parameters.");  CCASSERT(!listener->isRegistered(), "The listener has been registered.");    if (!listener->checkAvailable())    return;    listener->setSceneGraphPriority(node);  listener->setFixedPriority(0);  listener->setRegistered(true);    addEventListener(listener);}
      void EventDispatcher::addEventListenerWithFixedPriority(EventListener* listener, int fixedPriority){  CCASSERT(listener, "Invalid parameters.");  CCASSERT(!listener->isRegistered(), "The listener has been registered.");  CCASSERT(fixedPriority != 0, "0 priority is forbidden for fixed priority since it's used for scene graph based priority.");    if (!listener->checkAvailable())    return;    listener->setSceneGraphPriority(nullptr);  listener->setFixedPriority(fixedPriority);  listener->setRegistered(true);  listener->setPaused(false);  addEventListener(listener);}

      (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 to manually remove FixedPriority listener after it is added. SceneGraphPriority listener is bound to node and will be removed from node destructor.

      Removal method:
      dispatcher->removeEventListener(listener);

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.