Games like plants and botnets and defending Radishes have a slide screen selection level function at the beginning, which is very effective. More and more mobile games use this method to choose checkpoints, I will often use this function in the future, so I will encapsulate this function, let's take a look at the code. [cpp] # ifndef _ SLIDING_LAYER_H _ # define _ SLIDING_LAYER_H _///////////////////// //////////////////////////////////////// //// // class CSlidingLayer: public CCLayer {public: CSlidingLayer ();~ CSlidingLayer (); public: virtual bool ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent); // virtual void ccTouchMoved (CCTouch * pTouches, CCEvent * pEvent) when the finger is touched ); // virtual void ccTouchEnded (CCTouch * pTouch, CCEvent * pEvent) when the finger moves; // public: void AddSprite (CCSprite * pSprite) when the finger moves ); // Add the genie bool IsMoveLayer () {return m_bMoveLayer;} // used to determine whether the finger slides private: CCPoint m_ptTouchDown; // typ Edef vector <CCSprite *> VEC_SPRITE; VEC_SPRITE m_vecSprite; // sprite set int m_nCurSprite; // currently displayed genie bool m_bMoveLayer; // mobile layer }; # endif [cpp] # include "stdafx. h "# include" SlidingLayer. h "const int NEXT_SPRITE_SHOW = 50; // The distance between the touch point and the stop point. If this value is exceeded, it is regarded as a sliding screen. If the value is not exceeded, it is considered as a misoperation, do not perform slide screen processing //////////////////////////////////// /// // CSlidingLayer:: CSlidingLayer () {m_ptTouchDown.setPoint (-1 ,-1); m_nCurSprite = 0; m_bMoveLayer = false ;} //////////////////////////////////////// //// // CSlidingLayer: :~ CSlidingLayer () {}////////////////////////////////////// /// // bool CSlidingLayer:: ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent) {m_ptTouchDown = pTouch-> getLocation (); // obtain the touch point m_bMoveLayer = false; return true ;} //////////////////////////////////////// /// // void CSlidingLayer:: ccTouchMoved (CCTouch * pTouch, CCEvent * pEvent) {m_bMoveL Ayer = true; if (m_vecSprite.size () <= 0) {return;} int nCurSpriteTemp = m_nCurSprite; int nWillShowSpriteIndex = m_nCurSprite; // the index of the sprite to be displayed, it is the genie CCPoint ptTouch = pTouch-> getLocation (); if (ptTouch. x> m_ptTouchDown.x) // slide to the right {nWillShowSpriteIndex --;} else if (ptTouch. x <m_ptTouchDown.x) // slide to the left {nWillShowSpriteIndex ++;} nWillShowSpriteIndex = _ max (0, nWillShowSpriteIndex ); NWillShowSpriteIndex = _ min (nWillShowSpriteIndex, (int) m_vecSprite.size ()-1); rows = _ max (0, rows); nCurSpriteTemp = _ min (nCurSpriteTemp, (int) m_vecSprite.size ()-1); // int nDalta = ptTouch is displayed when the current page is moved. x-m_ptTouchDown.x; // The X difference between the current sliding point of the finger and the point of the first touch CCSprite * pSprite = m_vecSprite [nCurSpriteTemp]; CCAssert (pSprite! = NULL, ""); CCSize size = CCDirector: shareddire()-> getWinSize (); pSprite-> setPosition (ccp (size. width/2 + nDalta, size. height/2); // Move the current page if (nWillShowSpriteIndex! = Blank) // move the page to be displayed {CCSprite * pSpriteWillShow = m_vecSprite [nWillShowSpriteIndex]; MUSTOK (pSpriteWillShow); if (nWillShowSpriteIndex <nCurSpriteTemp) {pSpriteWillShow-> setPosition (ccp (-size. width/2 + nDalta, size. height/2); // when moving to the right, the page to be displayed is on the left of the screen, and the anchor is outside the half screen} else {pSpriteWillShow-> setPosition (ccp (size. width * 3/2 + nDalta, size. height/2); // when moving to the left, the page to be displayed is on the right of the screen, and the anchor is outside the 3/2 screen }}}// //////////////////////////////////////// /// // CSlidingLayer:: ccTouchEnded (CCTouch * pTouch, CCEvent * pEvent) {if (m_vecSprite.size () = 0) {return;} CCPoint ptEnd = pTouch-> getLocation (); if (ptEnd. x> m_ptTouchDown.x) // slide to the right {if (ptEnd. x-m_ptTouchDown.x> = NEXT_SPRITE_SHOW) {m_nCurSprite --; // the current page changes to the previous page} else if (ptEnd. x <m_ptTouchDown.x) // slide to the left {if (m_p TTouchDown. x-ptEnd. x> = NEXT_SPRITE_SHOW) {m_nCurSprite ++; // the current page changes to the next page} m_nCurSprite = _ min (m_nCurSprite, (int) m_vecSprite.size ()-1 ); m_nCurSprite = _ max (0, m_nCurSprite); if (m_vecSprite.size ()! = 0) {CCSprite * pSprite = m_vecSprite [m_nCurSprite]; CCAssert (pSprite! = NULL, ""); CCSize size = CCDirector: shareddire()-> getWinSize (); pSprite-> setPosition (ccp (size. width/2, size. height/2); // display the current page in the middle of the screen // to make the Left and Right genie do not affect the display of the current page, move the Left and Right sprite to a far away place. if (m_nCurSprite> = 1) {pSprite = m_vecSprite [m_nCurSprite-1]; pSprite-> setPosition (ccp (-10000, 0);} if (m_nCurSprite + 1 <= (int) m_vecSprite.size ()-1) {pSprite = m_vecSprite [m_nCurSprite + 1]; pSprite-> se TPosition (ccp (-10000, 0 ));}}} //////////////////////////////////////// /// // void CSlidingLayer:: AddSprite (CCSprite * pSprite) {CCAssert (pSprite! = NULL, ""); m_vecSprite.push_back (pSprite);} Step 1. Record the touch point when the finger is touched. 2. When the finger is sliding, determine whether it is left or right, move the current page. If you move the current page to the left, move the next page to the right. If you move the current page to the right, move the previous page to the right. 3. When you open your finger, determine the sliding distance, the current page is displayed based on the left and right slides. In addition, it is easy to move the genie on the left and right sides of the current page to avoid interfering with the display of the current page. You only need to derive your class from CSlidingLayer, then add CSlidingLayer: xxxxxx at the end of the three finger functions, which is the same as that of MFC. For example, the following three functions: [cpp] //// // //////////////////////////////////////// /bool CRossingMgr:: ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent) {CSlidingLayer: ccTouchBegan (pTouch, pEvent); return true ;} //////////////////////////////////////// /// // void CRossingMgr:: ccTouchMoved (CCTouch * pTouch, CCEvent * pEvent) {CSlidingLayer: ccTouchMoved (pTouch, pEvent );} //////////////////////////////////////// /// // void CRossingMgr:: ccTouchEnded (CCTouch * pTouch, CCEvent * pEvent) {CSlidingLayer: ccTouchEnded (pTouch, pEvent);} as follows: