Cocos2dx Click Event Analysis (3)
1. In cocos2dx Click Event Analysis (2), we have analyzed from the java end, single-handed touch and multi-hand touch screen. Num --- 1, whether single-touch or multi-touch, this value is 1ids [] --- IDvoid CCEGLViewProtocol: handleTouchesBegin (int num, int ids [], float xs [], float ys []) {CCSet set; for (int I = 0; I <num; ++ I) {int id = ids [I]; float x = xs [I]; float y = ys [I]; // static CCDictionary s_TouchesIntergerDict stores the finger Touch ID, each with one more hand, a new ID CCInteger * pIndex = (CCInteger *) s_TouchesIntergerDict.objectForKey (id); int nUnusedIndex = 0; // it is New touch if (pIndex = NULL) {nUnusedIndex = getUnUsedIndex (); // The touches is more than MAX_TOUCHES? If (nUnusedIndex =-1) {CCLOG ("The touches is more than MAX_TOUCHES, nUnusedIndex = % d", nUnusedIndex); continue;} // s_pTouches stores The touch information, in the CCTouch class, there is a m_nId member variable, and here it corresponds to nUnusedIndex // it corresponds to CCTouch * pTouch = s_pTouches [nUnusedIndex] = new CCTouch (); pTouch-> setTouchInfo (nUnusedIndex, (x-m_obViewPortRect.origin.x)/m_fScaleX, (y-example)/m_fScaleY); // CCLOG ("x = % f y = % f ", PTouch-> getLocationInView (). x, pTouch-> getLocationInView (). y); CCInteger * pInterObj = new CCInteger (nUnusedIndex); s_TouchesIntergerDict.setObject (pInterObj, id); set. adsdObject (pTouch); pInterObj-> release () ;}} if (set. count () = 0) {CCLOG ("touchesBegan: count = 0"); return;} // according to my analysis, here, set. the value of count () can only be 1. M_pDelegate-> touchesBegan (& set, NULL) ;}--> void CCTouchDispatcher: touchesBegan (CCSet * touches, CCEvent * pEvent) {if (m_bDispatchEvents) {this-> touches (touches, pEvent, CCTOUCHBEGAN) ;}}>//// dispatch events // void CCTouchDispatcher: touches (CCSet * pTouches, CCEvent * pEvent, unsigned int uIndex) {// content in this section, which will be analyzed later.} verify that the TestCpp example of cocos2dx contains MutiTouchTest. cpp example, you can print some information in that example. The following is the source code after I added some printed information: # include "MutiTouchTest. h "# include" cocos2d. h "static ccColor3B s_TouchColors [colors] = {ccYELLOW, ccBLUE, ccGREEN, ccRED, ccMAGENTA}; class TouchPoint: public CCNode {public: TouchPoint () {setShaderProgram (CCShaderCache :: sharedShaderCache ()-> programForKey (kCCShader_PositionTextureColor);} virtual void draw () {ccDrawColor4B (m_TouchColor.r, m_TouchColor.g, m_TouchColor. B, 255); glLineWidth (10); ccDrawLine (ccp (0, m_pTouchPoint.y), ccp (getContentSize (). width, m_pTouchPoint.y); ccDrawLine (ccp (m_pTouchPoint.x, 0), ccp (m_pTouchPoint.x, getContentSize (). height); glLineWidth (1); ccPointSize (30); ccDrawPoint (m_pTouchPoint);} void setTouchPos (const CCPoint & pt) {m_pTouchPoint = pt;} void setTouchColor (ccColor3B color) {m_TouchColor = color;} static TouchPoint * TouchPointWithParent (CCNode * pParent) {TouchPoint * pRet = new TouchPoint (); pRet-> setContentSize (pParent-> getContentSize (); pRet-> setAnchorPoint (ccp (0.0f, 0.0f); pRet-> autorelease (); return pRet;} private: CCPoint m_pTouchPoint; ccColor3B m_TouchColor;}; bool MutiTouchTestLayer: init () {if (CCLayer :: init () {setTouchEnabled (true); return true;} return false;} static CCDictionary s_dic; void Muti TouchTestLayer: vertex (void) {CCDirector: shareddire()-> getTouchDispatcher ()-> addStandardDelegate (this, 0);} void MutiTouchTestLayer: vertex (CCSet * pTouches, CCEvent * pEvent) {CCLog ("ccTouchesBegan ++ % d", pTouches-> count (); CCSetIterator iter = pTouches-> begin (); (; iter! = PTouches-> end (); iter ++) {CCTouch * pTouch = (CCTouch *) (* iter); TouchPoint * pTouchPoint = TouchPoint: touchPointWithParent (this ); CCPoint location = pTouch-> getLocation (); pTouchPoint-> setTouchPos (location); pTouchPoint-> setTouchColor (s_TouchColors [pTouch-> getID ()]); addChild (pTouchPoint ); s_dic.setObject (pTouchPoint, pTouch-> getID (); CCLog ("ccTouchesBegan ++ ID ++ % d", pTouch-> getID () ;}} void MutiTouchTestLayer: ccTouchesMoved (CCSet * pTouches, CCEvent * pEvent) {CCLog ("ccTouchesMoved ++ % d", pTouches-> count ()); CCSetIterator iter = pTouches-> begin (); for (; iter! = PTouches-> end (); iter ++) {CCTouch * pTouch = (CCTouch *) (* iter); TouchPoint * pTP = (TouchPoint *) s_dic.objectForKey (pTouch-> getID (); CCPoint location = pTouch-> getLocation (); pTP-> setTouchPos (location ); CCLog ("ccTouchesMoved ++ ID ++ % d", pTouch-> getID () ;}} void MutiTouchTestLayer: ccTouchesEnded (CCSet * pTouches, CCEvent * pEvent) {CCSetIterator iter = pTouches-> begin (); for (; iter! = PTouches-> end (); iter ++) {CCTouch * pTouch = (CCTouch *) (* iter); TouchPoint * pTP = (TouchPoint *) s_dic.objectForKey (pTouch-> getID (); removeChild (pTP, true); updated (pTouch-> getID () ;}} void MutiTouchTestLayer: ccTouchesCancelled (CCSet * pTouches, CCEvent * pEvent) {ccTouchesEnded (pTouches, pEvent);} void metadata: runThisTest () {MutiTouchTestLayer * pLayer = MutiTouchTestLayer: create (); addChild (pLayer, 0 ); CCDirector: sharedDirector ()-> replaceScene (this);} 1, 03-30 13:13:14. 192: D/cocos2d-x debug info (22995): ccTouchesBegan ++ 103-30 13:13:14. 192: D/cocos2d-x debug info (22995): ccTouchesBegan ++ ID ++ 003-30 13:13:14. 312: D/cocos2d-x debug info (22995): ccTouchesBegan ++ 103-30 13:13:14. 312: D/cocos2d-x debug info (22995): ccTouchesBegan ++ ID ++ 12, 03-30 13:13:14. 773: D/cocos2d-x debug info (22995): ccTouchesMoved ++ 203-30 13:13:14. 773: D/cocos2d-x debug info (22995): ccTouchesMoved ++ ID ++ 103-30 13:13:14. 773: D/cocos2d-x debug info (22995): ccTouchesMoved ++ ID ++ 0
Conclusion: In the ccTouchesBegan function, even multi-touch operations only have one touch event at a time, but different IDs are assigned to the touch events of the first and second fingers. In the ccTouchesMoved function, it also transmits the touch information of all vertices of multi-touch.