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
Where do I need to enable touch? In init, for example
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