Cocos2d-x Touch Screen Response (single touch) cctouchbegan,cctouchmove,cctouchend

Source: Internet
Author: User

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

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.