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:
- -(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.
- -(void) cctouchesended: (Nsset *) touches withevent: (Uievent *) event;
- -(void) cctouchesmoved: (Nsset *) touches withevent: (Uievent *) event;
- -(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:
- Remember to import "CCTouchDispatcher.h" in the header file.
- -(void) Registerwithtouchdispatcher {
- [[Cctouchdispatcher Shareddispatcher] addtargeteddelegate:self priority:0 Swallowstouches:yes];
- }
The user needs to overload 4 main processing methods in the targeted mode. The Cctouchbegan must be rewritten , and the other three are optional.
- -(BOOL) Cctouchbegan: (Uitouch *) Touch withevent: (uievent *) event; (Must be implemented)
- -(void) cctouchmoved: (Uitouch *) Touch withevent: (uievent *) event;
- -(void) cctouchended: (Uitouch *) Touch withevent: (uievent *) event;
- -(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
- [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)