The touch mechanism in COCOS2DX 3.2

Source: Internet
Author: User

In version 3.2 of COCOS2DX, the previous 2.x version was deprecated, so let's take a look at a piece of code in Layer.h

 //Single Touch  Virtual BOOLOntouchbegan (Touch *touch, Event *unused_event);  Virtual voidOntouchmoved (Touch *touch, Event *unused_event);  Virtual voidontouchended (Touch *touch, Event *unused_event);  Virtual voidOntouchcancelled (Touch *touch, Event *unused_event);  //multi-touch  Virtual voidOntouchesbegan (Const STD:: vector<Touch*>& Touches, Event *unused_event);  Virtual voidOntouchesmoved (Const STD:: vector<Touch*>& Touches, Event *unused_event);  Virtual voidOntouchesended (Const STD:: vector<Touch*>& Touches, Event *unused_event);  Virtual voidOntouchescancelled (Const STD:: vector<Touch*>&touches, Event *unused_event);

Single Touch: (That is, only the registered layer can receive touch events)

Ontouchbegan: If true: Subsequent touch events on this layer can be triggered and block the backward layer passing

If False, subsequent touch events on this layer cannot be triggered and passed backwards

In simple terms, if

1.Layer only one layer of the situation:

virtual bool Ontouchbegan (Cctouch *ptouch, ccevent *pevent);

A. return false, Cctouchmoved (), cctouchended () no longer receive messages

B. Returns true, Cctouchmoved (), cctouchended () can receive a message

2.Layer has multi-layered conditions:

virtual bool Ontouchbegan (Cctouch *ptouch, ccevent *pevent);

A. return false, ontouchmoved () of this layer, ontouchended () will no longer receive messages, but other layers below this layer will receive messages

B. Returns true if the Ontouchmoved (), ontouchended () of this layer can receive the message, but the other layers below this layer no longer receive the message

Single Touch Simple usage:

Add the following code to the layer, overriding the ONTOUCHXXX function

Auto Dispatcher = Director::getinstance ()->geteventdispatcher ();  Auto listener = eventlistenertouchonebyone::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->setswallowtouches (TRUE);//Do not pass down touch  Dispatcher->addeventlistenerwithscenegraphpriority (Listener,this);

Listener->setswallowtouches (True), do not touch down, simple point, for example, there are two sprites, a and b,a in the upper B (position overlap), touch A, B will not be affected

  Listener->setswallowtouches (false) Conversely, passing the touch down, touching a also equals touching the B


Multi-Touch Point single usage (multiple Layer capture 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 rewrite the ONTOUCHSXXX function of the layer

about Eventdispatcher:

    • Get method:

    • auto dispatcher = Director::getinstance ()->geteventdispatcher (); 

Event listeners include the following:

  • Touch Events (Eventlistenertouch)
  • Keyboard response Events (Eventlistenerkeyboard)
  • Speeding up Logging events (eventlisteneracceleration)
  • Mouse Response Events (Eventlistenermouse)
  • Custom Events (Eventlistenercustom)

    The above event listeners are unified for _eventDispatcher management.


Priority:

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

2. If the priority is the same, the upper level (z-axis) first receives the touch event

There are two ways to add event listener Listener1 to the event scheduler _eventdispatcher:

void Eventdispatcher::addeventlistenerwithscenegraphpriority (eventlistener* Listener, node* Node)    void int fixedpriority)
The code expands:
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,intfixedpriority) {  Ccassert (Listener,"Invalid parameters.");  Ccassert (!listener->isregistered (),"The listener has been registered.");  Ccassert (fixedpriority! =0,"0 priority was forbidden for the 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 event listener priority for Addeventlistenerwithscenegraphpriority is 0, and the priority of event listeners in Addeventlistenerwithfixedpriority cannot be set to 0, because this is reserved for scenegraphpriority use.


(2) In addition, it is very important, fixedpriority listener after the addition of the need to manually remove, and scenegraphpriority listener is bound to node, in the destructor of node will be removed.

Removal method:
Dispatcher->removeeventlistener (listener);

The touch mechanism in COCOS2DX 3.2

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.