COCOS2DX Touch Cctouch A single touch of the class has four functions Cctouchbegan,cctouchmove,cctouchend, Cctouchcancalled.
The functions of these touches do not necessarily respond to each other, but Cctouchbegan must have a return value of bool, and the return value of the other function is void
Below we look at how to touch:
First we create a new project Hello.
1. Hello.h declaration function in source file
1 voidRegisterwithtouchdispatcher (void);//Register Touch2 BOOLCctouchbegan (Cctouch *ptouch, ccevent *pevent);//Touch Start, note the return type, if return false, do not write the following three functions3 voidCctouchmoved (Cctouch *ptouch, ccevent *pevent);//Touch Slide4 voidcctouchended (Cctouch *ptouch, ccevent *pevent);//Touch End5 voidCctouchcancelled (Cctouch *ptouch, ccevent *pevent);//touch cancel such as halfway call
2. Turn on Touch
Join the line where you need to turn on the touch, such as Init
1 BOOL Hello::init () { settouchenabled (true); return true; }
3. Implement the Registration function
1 void hello::registerwithtouchdispatcher () 2 {3 Ccdirector::shareddirector ()->gettouchdispatcher ()->addtargeteddelegate (this0True ); 4 }
4. Realize Cctouchbegan
1 bool Hello::cctouchbegan ( Cctouch *ptouch, ccevent *pevent) 2 { 3 ccpoint touchpoint = Ptouch->getlocation (); // get touch coordinates 4 cclog ( " touch began, Touchpoint is%f " 5 return true ; // true indicates that the continuation response Cctouchmove,cctouchend,cctouchcancalled,false is not responding. 6 }
5. Realize Cctouchmove
1 void Hello::cctouchbegan (Cctouch *ptouch, ccevent *pevent)2{ 3 ccpoint touchpoint = Ptouch->getlocation (); // Get Touch coordinates 4 Cclog ("touch Move, touchpoint is%f", touchpoint); 5 }
6. Realize cctouchended
1 void hello::cctouchended (Cctouch *ptouch, ccevent *pevent)2{3 // 4 cclog ("touch End, touchpoint is%f", touchpoint); 5 }
7. Realize cctouchcancalled
1 void Hello::cctouchcancalled (Cctouch *ptouch, ccevent *pevent)2{ 3 ccpoint touchpoint = Ptouch->getlocation (); // Get Touch coordinates 4 Cclog ("touch End, touchpoint is%f", touchpoint); 5 }
Cocos2d-x Touch Screen Response (single touch) cctouchbegan,cctouchmove,cctouchend