Cocos2d-x level selection interface (use of CCScrollView)

Source: Internet
Author: User

Today, I want to write a level selection interface.

Baidu adopts CCScrollView.

The usage of the CCScrollView is not described. Baidu. (Keyword: CCScrollView details)

Here, we mainly use an example to introduce the implementation of the menu interface.

First. As follows:


1. First from http://pan.baidu.com/share/link? Required id = 2511857370 & uk = 2685725110 download required resources

2. Import the image Resources and CCGameScrollView. h and CCGameScrollView. cpp files in the Resources folder to your project.

3. Let your class inherit CCGameScrollView and implement five pure virtual interfaces.

// The following five methods must inherit // rolling trigger virtual void scrollViewDidScroll (cocos2d: extension: CCScrollView * view); // scaling trigger virtual void scrollViewDidZoom (cocos2d: extension:: CCScrollView * view); // initialize each separate page. pPage is the container for this page. nPage is the IDvirtual bool scrollViewInitPage (cocos2d: CCNode * pScroll, cocos2d :: CCNode * pPage, int nPage); // click a page to process virtual void scrollViewClick (const cocos2d: CCPoint & oOffset, const cocos2d: CCPoint & oPoint, cocos2d :: CCNode * pPage, int nPage); // The callback that ends after each slide. You can handle some things here: virtual void scrollViewScrollEnd (cocos2d: CCNode * pPage, int nPage );

4. implement related methods.


The following describes my test classes.


LevelSelect. h

/// LevelSelect. h // RunGame // Created by Colin on 14-4-13. /// # ifndef _ RunGame _ LevelSelect __# define _ RunGame _ LevelSelect __# include
 
  
# Include "cocos2d. h "# include" CCGameScrollView. h "// import the header file using namespace cocos2d; class LevelSelectScene: public cocos2d: CCLayer, public CCCGameScrollViewDelegate {public: LevelSelectScene ();~ LevelSelectScene (); bool init (); static cocos2d: CCScene * scene (); // The following five methods must be inherited // rolling trigger virtual void scrollViewDidScroll (cocos2d: extension:: CCScrollView * view); // scaling triggers virtual void scrollViewDidZoom (cocos2d: extension: CCScrollView * view); // initializes each individual page, and pPage is the container of this page, nPage is the IDvirtual bool scrollViewInitPage (cocos2d: CCNode * pScroll, cocos2d: CCNode * pPage, int nPage) of this page. // click a page to process virtual void scrollViewClick (const cocos2d:: CCPoint & oOffset, const cocos2d: CCPoint & oPoint, cocos2d: CCNode * pPage, int nPage); // The callback that ends after each slide, here we can handle some things: virtual void scrollViewScrollEnd (cocos2d: CCNode * pPage, int nPage); CREATE_FUNC (LevelSelectScene); private: CCCGameScrollView * m_ScrollView ;}; # endif/* defined (_ RunGame _ LevelSelect __)*/
 



LevelSelect. cpp

/// LevelSelect. cpp // RunGame /// Created by Colin on 14-4-13. //// # include "LevelSelect. h "# include" SimpleAudioEngine. h "# include" GameMain. h "// main game interface using namespace cocos2d; using namespace CocosDenshion; USING_NS_CC_EXT; LevelSelectScene: LevelSelectScene () {} LevelSelectScene ::~ LevelSelectScene () {}ccscene * LevelSelectScene: scene () {CCScene * scene = CCScene: create (); LevelSelectScene * layer = LevelSelectScene: create (); scene-> addChild (layer); return scene;} // scroll triggered void LevelSelectScene: scrollViewDidScroll (CCScrollView * view) {}// scaling triggered void LevelSelectScene :: scrollViewDidZoom (CCScrollView * view) {} // initialize each separate page. pPage is the container of this page. nPage is the IDbool LevelSelectScene: scrollView of this page. InitPage (cocos2d: CCNode * pScroll, cocos2d: CCNode * pPage, int nPage) {CCString str; str. initWithFormat ("12703d.png", nPage + 1); CCSprite * sprite = CCSprite: create (str. getCString (); pPage-> addChild (sprite); return true;} // click the processing void LevelSelectScene: scrollViewClick (const cocos2d: CCPoint & oOffset, const cocos2d: CCPoint & oPoint, cocos2d: CCNode * pPage, int nPage) {switch (nPage) {case 0: {// enter The main game interface CCDirector: shareddire()-> replaceScene (CCTransitionFadeBL: create (0.5, GameMain: scene ();} break; case 1: {// scroll to a page m_ScrollView-> scrollToPage (2);} break; case 2: {// set m_ScrollView-> setCurPage (3);} break ;}} // The callback that ends after each slide. You can handle some things here. void LevelSelectScene: scrollViewScrollEnd (cocos2d: CCNode * pPage, int nPage) {CCLog ("Current Page = % d", nPage);} bool LevelSelectScene: init () {if (! CCLayer: init () {return false;} // CCScrollViewm_ScrollView = CCCGameScrollView: create (); // create a scrollviewm_ScrollView-> setDirection (kCCScrollViewDirectionHorizontal ); // set the scroll direction. Currently, only the horizontal and vertical directions are supported. // this indicates the number of pages and the size of each page (which affects the distance between pages) m_ScrollView-> createContainer (this, 5, CCSizeMake (CCDirector: sharedDirector ()-> getVisibleSize (). width * 0.8, CCDirector: shareddire()-> getVisibleSize (). height); // generally the origin m_ScrollView-> setPosition (ccp (0, 0); // the size of the view (usually the size of the screen) m_ScrollView-> setViewSize (CCDirector: shareddire()-> getVisibleSize (); this-> addChild (m_ScrollView); return true ;}



On the way to learning, I will share with you.

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.