Cocos2d-x 3.2 Monopoly game project development-Part 5 standalone game-levels choose ScrollView, monopoly Standalone
Click MenuScene. cpp to call
Director: getInstance ()-> pushScene (MapChooseScene: createScene ());
Go to the level selection interface. We use the ScrollView control to create this interface.
Because the position of the scrollview is relatively random after being dragged, I think it is to drag it to the second image to display the second image completely. Do not deviate from the position, therefore, after moving, you need to correct the position and write an adjustScrollView () method to adjust the position.
The Code is as follows:
MapChooseScene. h content:
Const int MAP_COUNT = 3; // defines three level pictures. const int TOUCH_DISTANCE = 50; // gesture sliding distance from class MapChooseScene: public Layer, public ScrollViewDelegate // use scrollview to inherit this class {................ Private: Size visibleSize; Layer * _ spritesContainer; // The sprite container int currentPage used to store three levels of images; // The Point beginTouchPoint where the current level map is located; // click the position ScrollView * scrollView when you press it. // The scrollview Object void singleTouchDown (Object * pSender, Control: EventType event); void addBackgroundSprite (); // Add the background method void addScrollView (); // Add the scrollview object and its sprite // inherit the three methods void scrollViewDidScroll (ScrollView * view) to be implemented by the ScrollViewDelegate class ); void scrollViewDidZoom (ScrollView * view); void scrollViewMoveOver (ScrollView * view); void adjustScrollView (float distance); // custom method, used to adjust the position after dragging void onTouchEnded (Touch * touch, Event * unused_event); bool onTouchBegan (Touch * touch, Event * unused_event); void onTouchMoved (Touch * touch, event * unused_event );};
MapChooseScene. cpp file content:
Bool MapChooseScene: init () {if (! Layer: init () {return false;} visibleSize = Director: getInstance ()-> getVisibleSize (); currentPage = 1; // The default value is the first level image addBackgroundSprite (); // Add the background addScrollView (); // Add the scrollview // Add the Touch listener setTouchMode (Touch: DispatchMode :: ONE_BY_ONE); auto listener = listener: create (); listener-> setSwallowTouches (true); listener-> onTouchBegan = CC_CALLBACK_2 (MapChooseScene: onTouchBegan, this ); listener-> onTouchMoved = CC_CALLBACK_2 (MapChooseScene: onTouchMoved, this); listener-> onTouchEnded = CC_CALLBACK_2 (listener: onTouchEnded, this); ctor: getInstance () -> getEventDispatcher ()-> addEventListenerWithSceneGraphPriority (listener, this); return true ;}
The addScrollView () method is used to create three levels of sprite images and add them to the Layer-class container spritesContainer object for horizontal display.
Void MapChooseScene: addScrollView () {_ spritesContainer = Layer: create (); // create a map sprite and add it to spritesContainer Sprite * beachSprite = Sprite: create (BEACH_ITEM ); sprite * seaSprite = Sprite: create (SEA_ITEM); Sprite * moonSprite = Sprite: create (MOON_ITEM); _ spritesContainer-> addChild (beachSprite ); beachSprite-> setPosition (ccpAdd (center, ccp (0, 0); _ spritesContainer-> addChild (seaSprite); seaSprite-> setPosition (ccpAdd (center, ccp (visibleSize. width, 0); _ spritesContainer-> addChild (moonSprite); moonSprite-> setPosition (ccpAdd (center, ccp (2 * visibleSize. width, 0); _ spritesContainer-> setPosition (CCPointZero); _ spritesContainer-> setContentSize (CCSize (visibleSize. width * MAP_COUNT, visibleSize. height); // container size // create a scrollView object and set scrollView = ScrollView: create (); scrollView-> setContainer (_ spritesContainer ); scrollView-> setDirection (ScrollView: Direction: HORIZONTAL); // horizontally displayed scrollView-> setTouchEnabled (true); scrollView-> setPosition (CCPointZero ); scrollView-> setViewSize (CCSizeMake (visibleSize); // scrollView-> setContentOffset (CCPointZero, true); scrollView-> setContentSize (CCSize (visibleSize. width * MAP_COUNT, visibleSize. height); // scrollview the same size as spritesContainer scrollView-> setDelegate (this); scrollView-> setBounceable (false); addChild (scrollView );}
Press beginTouchPoint and assign bool MapChooseScene: onTouchBegan (Touch * touch, Event * unused_event) {beginTouchPoint = Director: getInstance ()-> convertToGL (touch-> getLocationInView ()); log ("touch begain"); return true;} // After the Touch ends, obtain the touch distance and call the Image Position Correction Method adjustScrollViewvoid MapChooseScene: onTouchEnded (touch * Touch, event * unused_event) {Point endPoint = Director: getInstance ()-> convertToGL (touch-> getLocationInView (); float distance = endPoint. x-beginTouchPoint. x; if (fabs (distance)> TOUCH_DISTANCE) {adjustScrollView (distance );}}
Correction Method: Based on the Distance Difference between the position of the touch sliding, it is confirmed that the sliding is left or right. If it is sliding to the left, the current page increments; otherwise, the minimum value is 1 and the maximum value is 3.
Then, set the Offset of scrollview Based on the page on which the image is displayed to enable location correction.
void MapChooseScene::adjustScrollView(float offset){ if (offset<0) { currentPage ++; }else { currentPage --; } if (currentPage <1) { currentPage = 1; } if(currentPage > MAP_COUNT) { currentPage = MAP_COUNT; } CCPoint adjustPos = ccp(- visibleSize.width * (currentPage-1), 0); scrollView->setContentOffset(adjustPos, true);}
Click Download Code address http://download.csdn.net/detail/lideguo1979/8268033
To be continued ..............................