Cclayer in touch events (standard touch Delegate and targeted touch Delegate)

Source: Internet
Author: User

In doing exercises, touch the fault and see the source code to understand the next touch event.

Practice operation: Straight Cclayer subclass Init in this->settouchenabled (true);

Event handling method overrides

virtual bool Cctouchbegan (cctouch* Touch, ccevent* event), virtual void cctouchmoved (cctouch* touch, ccevent* event); virtual void cctouchended (cctouch* touch, ccevent* event);

The button didn't respond.

Due to settouchenabled (true); Turn on multi-touch, and the event handling method is for a single point, so no.

Workaround 1.

Multi-point processing method for overlay events

Default implements is used to call script callback if exist    virtual void Cctouchesbegan (Ccset *ptouches, Ccevent * pevent);   virtual void cctouchesmoved (Ccset *ptouches, ccevent *pevent);    virtual void cctouchesended (Ccset *ptouches, ccevent *pevent);   virtual void cctouchescancelled (Ccset *ptouches, ccevent *pevent);

From the parameter type Ccset It is possible to see that this is a collection, which should be a multiple-click point.

Workaround 2.

Overlay OnEnter (), plus single-point event entrusted

OnEnter () {ccdirector* pdirector = Ccdirector::shareddirector ();pD irector->gettouchdispatcher () Addtargeteddelegate (this, 0, true); Cclayer::onenter ();//This need to add}cclayer::onenter () {....    if (m_btouchenabled)//This m_btouchenabled is settouchenabled (true) set the    {        this-> Registerwithtouchdispatcher ();//Set Standard Touch Delegate, which is why cclayer defaults to the Way    } ...}

Touch Event Distribution Order

Cocos2d-x first dispatched the event to Cctargetedtouchdelegate. Re-distribute the incident to Ccstandardtouchdelegate. For the same type of touchdelegate, it is based on the priority of the brochure

To determine the order of dispatch. Assuming the same priority, the event is distributed in accordance with the order of the Register.

--------------------------------------------------------------------------------------------------------------- ----------

Here are some of the http://www.cnblogs.com/pengyingh/articles/2435160.html that others have summed up

Two touch handling methods are available in COCOS2D development, Standardtouch Delegate and targeted touch Delegate Mode (see CCTouchDelegateProtocol.h in the source code), Cclayer by default is the first way (see Cclayer Registerwithtouchdispatcher method).

You want to be able to receive a touch event in a cclayer subclass. You need to activate touch support first. Set the istouchenabled value to Yes in the Init method.

Standard Touch Delegate (cclayer default to this way)

The standard method requires the user to reload four main touch processing methods, such as the following:

    1. -(void) Cctouchesbegan: (Nsset *) touches withevent: (Uievent *) event;

When the touch event occurs. The method is called to respond to the touch event. Given a single touch, you can get a touch object simply by calling Uitouch *touch = [touches anyobject]. Suppose you need to respond to multi-touch. You need to call [[Event Alltouches] allobjects] to return a Uitouch Nsarray object. Then use Nsarray's objectatindex to visit each Uitouch object in turn.

To get the coordinates of the Uitouch object (if the Uitouch name is called touch), calling [Touch locationinview: [Touch view]] Returns a uiview related coordinate viewpoint.

When you create a new COCOS2D application using the COCOS2D New Application Wizard, In the Applicationdidfinishlaunching method of the Xxxappdelegate class Ccdirector converts uiview to Eaglview that support OpenGL ES.

At this time We also need to convert the ViewPoint in the previously acquired UIView to Eaglview coordinates, and call [[Ccdirector Shareddirector] converttogl:viewpoint] to implement.

    1. -(void) cctouchesended: (Nsset *) touches withevent: (Uievent *) event;
    2. -(void) cctouchesmoved: (Nsset *) touches withevent: (Uievent *) event;
    3. -(void) cctouchescancelled: (nsset*) Touch withevent: (uievent *) event;

These three methods are similar to those of Cctouchesbegan.

Targeted Touch Delegate Mode

In the standard mode, the response processing event is Nsset, while the targeted method only deals with a single Uitouch object, and under multi-touch conditions, the standard method should be adopted. You need to rewrite the Registerwithtouchdispatcher method in Cclayer before using the targeted mode:

    1. Remember to import "CCTouchDispatcher.h" in the header file.
    2. -(void) Registerwithtouchdispatcher {
    3. [[Cctouchdispatcher Shareddispatcher] addtargeteddelegate:self priority:0 Swallowstouches:yes];
    4. }

The user needs to overload 4 main processing methods in the targeted mode. The Cctouchbegan must be rewritten , and the other three are optional.

    1. -(BOOL) Cctouchbegan: (Uitouch *) Touch withevent: (uievent *) event; (Must be implemented)
    2. -(void) cctouchmoved: (Uitouch *) Touch withevent: (uievent *) event;
    3. -(void) cctouchended: (Uitouch *) Touch withevent: (uievent *) event;
    4. -(void) cctouchcancelled: (Uitouch *) Touch withevent: (uievent *) event;

Each time a touch event occurs, the Cctouchbegan method is called first, which responds to each Uitouch and returns a bool value. If yes, then perhaps Cctouchmoved, cctouchenabled, and cctouchcancelled will respond.

Multi-Touch support

Add the following code to the Applicationdidfinishlaunching method of the Xxxappdelegate class

    1. [Glview Setmultipletouchenabled:yes];
the swallowstouches in Addtargeteddelegate:selfAbout Swallowstouches

[[Cctouchdispatcher Shareddispatcher] addtargeteddelegate:self priority:kccmenutouchpriority SwallowsTouches:YES];

Suppose Swallowstouches:yes && touch begin return YES

Then his move and end are accepted. , the other classes are no longer acceptable.

Suppose Swallowstouches:no &&begin return Yes

Then his move and end are accepted. Other classes can still be accepted.


Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Cclayer in touch events (standard touch Delegate and targeted touch Delegate)

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.