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);}}