cocos2d-x-3.0 Touch Screen event (single touch)

Source: Internet
Author: User
Tags touch

Recently in learning Cocos2dx, from the Internet to download a call Zhao Yun to combat the source code, to compile the time Gettouchdispatcher () method can not find, so go to find degrees Niang, on this occasion just learn cocos2d-x-3.0 touch screen Event Touch screen event

Solve the above problem: Gettouchdispatcher () is a method in the 2.0 era, abandoned after 3.0

2.0 Times:

By default Cclayer does not start touch events, so you need to this->settouchenabled (true) in the initialization function; Enable touch event handling.

1. First, you need to register the mechanism for handling touch events in the Registerwithtouchdispatcher () method.

void Helloworld::registerwithtouchdispatcher ()
{
Standard Touch
Ccdirector::shareddirector ()->gettouchdispatcher ()->addstandarddelegate (this, 0);
Targeted Touch
Ccdirector::shareddirector ()->gettouchdispatcher ()->addtargeteddelegate (this, 0, true);
}

Default Cclayer default Registerwithtouchdispatcher implementation is registered as Ccstandardtouchdelegate, in addition, Cannot call Registerwithtouchdispatcher on its own, but should call this->settouchenabled (true)

2. Implement callback function

(1) Standard Touch

virtual void Cctouchesbegan (Ccset *ptouches, ccevent *pevent); Handling Pressed Events
virtual void cctouchesmoved (Ccset *ptouches, ccevent *pevent); Handling pressing and moving events
virtual void cctouchesended (Ccset *ptouches, ccevent *pevent); Handling Release Events
virtual void cctouchescancelled (Ccset *ptouches, ccevent *pevent); Handling Interrupt Events

(2) Target Touch

The cctargetedtouchdelegate consists of the following four callback functions:

virtual bool Cctouchbegan (Cctouch *ptouch, ccevent *pevent); Handles the user press event, true to continue processing, or false.

virtual void cctouchmoved (Cctouch *ptouch, ccevent *pevent); Handling pressing and moving events

virtual void cctouchended (Cctouch *ptouch, ccevent *pevent); Handling Release Events

virtual void cctouchcancelled (Cctouch *ptouch, ccevent *pevent); Handling Interrupt Events

There is a big difference between cctargetedtouchdelegate and ccstandardtouchdelegate.

3.0 times:

The idea is to encapsulate a rocker into a class and then look after the inherited cclayer to respond to touch events.

Source:

void Hrocker::startrocker (bool _isstopother)
{
Ccsprite *rocker = (ccsprite*) this->getchildbytag (Tag_rocker);
Rocker->setvisible (TRUE);
Ccsprite *ROCKERBG = (Ccsprite *) This->getchildbytag (TAG_ROCKERBG);
Rockerbg->setvisible (TRUE);
Ccdirector::shareddirector ()->gettouchdispatcher ()->addtargeteddelegate (This,-1,_isstopother);
}

2.0 Times code, compilation not past

After the code is changed:

void Hrocker::startrocker (bool _isstopother)
{
Ccsprite *rocker = (ccsprite*) this->getchildbytag (Tag_rocker);
Rocker->setvisible (TRUE);
Ccsprite *ROCKERBG = (Ccsprite *) This->getchildbytag (TAG_ROCKERBG);
Rockerbg->setvisible (TRUE);
Auto Listener1 = Eventlistenertouchonebyone::create ();
Listener1->ontouchbegan = Cc_callback_2 (hrocker::ontouchbegan,this);
listener1->ontouchmoved = Cc_callback_2 (hrocker::ontouchmoved,this);
listener1->ontouchended = Cc_callback_2 (hrocker::ontouchended,this);
Listener1->setswallowtouches (TRUE);
_eventdispatcher->addeventlistenerwithscenegraphpriority (Listener1,this);
}

The above is the realization of 3.0 single touch

Note the point:

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

2.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 has only one level of the situation:
virtual bool Ontouchbegan (Cctouch *ptouch, ccevent *pevent); A. Returns false, Cctouchmoved (), cctouchended () no longer receives message B. Returns True, Cctouchmoved (), cctouchended () can receive messages

(2) layer has multi-layered situation: 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 message B. Returns true if the ontouchmoved () of this layer, ontouchended () Messages can be received, but other layers below this layer are no longer able to receive messages

3.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



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.