Cocos2dx touch screen response (single touch) cctouchbegan, cctouchmove, cctouchend

Source: Internet
Author: User

Today, I will share with you the cocos2dx single-point touch experience.

Cocos2dx touch cctouch has four functionsCctouchbegan, cctouchmove, cctouchend, and cctouchcancalled.

These touch functions do not have to respond to every one of them, but cctouchbegan is required. Its return value is bool, And the return value of other functions is void.

Let's take a look at how to touch:

First, create a project hello

1. Declare the function in the source file hello. h.

Void registerwithtouchdispatcher (void); // Note whether to touch bool cctouchbegan (cctouch * ptouch, ccevent * pevent); // start with a touch. Note the return type. If false is returned, you do not need to write the following three functions: void cctouchmoved (cctouch * ptouch, ccevent * pevent); // touch and slide void cctouchended (cctouch * ptouch, ccevent * pevent ); // touch the end void cctouchcancelled (cctouch * ptouch, ccevent * pevent); // if the end point is to be reached

2. Enable touch

Add it to the place where you need to enable touch, such as init.

bool Hello::init(){    setTouchEnabled(true);    return true;}


3. Implement the Attention Function

void Hello::registerWithTouchDispatcher()           {    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);}


4. Implement cctouchbegan

Bool Hello: cctouchbegan (cctouch * ptouch, ccevent * pevent) {ccpoint touchpoint = ptouch-> getlocation (); // get the touch coordinate cclog ("Touch began, touchpoint is % F ", touchpoint); Return true; // true indicates continuing to respond to cctouchmove, cctouchend, cctouchcancalled, and false indicates no response .}


5. Implement cctouchmove
Void Hello: cctouchbegan (cctouch * ptouch, ccevent * pevent) {ccpoint touchpoint = ptouch-> getlocation (); // get the touch coordinate cclog ("Touch Move, touchpoint is % F ", touchpoint );}

6. Implement cctouchended

Void Hello: cctouchended (cctouch * ptouch, ccevent * pevent) {ccpoint touchpoint = ptouch-> getlocation (); // get the touch coordinate cclog ("Touch end, touchpoint is % F ", touchpoint );}

7. Implement cctouchcancalled

Void Hello: cctouchcancalled (cctouch * ptouch, ccevent * pevent) {ccpoint touchpoint = ptouch-> getlocation (); // get the touch coordinate cclog ("Touch end, touchpoint is % F ", touchpoint );}


OK. The touch is complete. Please kindly advise!




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