Cocos2d-x multi-point touch

Source: Internet
Author: User

The procedure of multi-touch is similar to that of single-touch. setTouchEnabled (true) enables the touch function, registers a touch event, and reloads multiple-touch functions: ccTouchesBegan (start) and ccTouchesMoved (MOVE), ccTouchesEnded (end), and ccTouchesCancelled (system interrupt cancel touch event). Each multi-touch function can retrieve all contacts by traversing the entire CCSet.

 

Program code:

Added the touch overload function to the header file:

              virtual void registerWithTouchDispatcher(void);virtual void ccTouchesBegan(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent);virtual void ccTouchesMoved(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent);virtual void ccTouchesEnded(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent);virtual void ccTouchesCancelled(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent);

 

Add the cpp file:

setTouchEnabled(true);

 

Define and implement these functions:

void HelloWorld::registerWithTouchDispatcher(){CCDirector *pdir = CCDirector::sharedDirector();pdir->getTouchDispatcher()->addStandardDelegate(this, 0);}void HelloWorld::ccTouchesBegan(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent){CCSetIterator iter = pTouches->begin();for (; iter != pTouches->end(); iter++){CCTouch* pTouch = (CCTouch*)(*iter);CCPoint location = pTouch->getLocation();CCLOG("ccTouchesBegan x: %f, y: %f", location.x, location.y);}}void HelloWorld::ccTouchesEnded(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent){CCSetIterator iter = pTouches->begin();for (; iter != pTouches->end(); iter++){CCTouch* pTouch = (CCTouch*)(*iter);CCPoint location = pTouch->getLocation();CCLOG("ccTouchesEnded x: %f, y: %f", location.x, location.y);}}void HelloWorld::ccTouchesMoved(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent){CCSetIterator iter = pTouches->begin();for (; iter != pTouches->end(); iter++){CCTouch* pTouch = (CCTouch*)(*iter);CCPoint location = pTouch->getLocation();CCLOG("ccTouchesMoved x: %f, y: %f", location.x, location.y);}}void HelloWorld::ccTouchesCancelled(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent){CCSetIterator iter = pTouches->begin();for (; iter != pTouches->end(); iter++){CCTouch* pTouch = (CCTouch*)(*iter);CCPoint location = pTouch->getLocation();CCLOG("x: %f, y: %f", location.x, location.y);}}





 

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.