Cocos2d-X3.0 achieves unlimited rolling of Map
Recently, I was playing a parkball game, where I used unlimited scrolling of maps in parkball games. After I checked a lot of information on the Internet, I also understand how to implement unlimited map scrolling.
In order to better illustrate the infinite scrolling of the map, I drew several images.
First, you need to prepare two maps, and put the first map on the window during initialization, and the second map on the back of the first map.
When the rightmost side of the first map overlaps with the leftmost side of the window
Place the first map behind the second map
When the rightmost side of the second map is at the leftmost side of the window
Place the second map behind the first map (the rightmost and leftmost sides of the first map overlap)
The above is the implementation logic of the infinite loop scrolling of the map. The following code is used to implement the infinite scrolling of the map.
First create a SceneMap class and add the following code in SceneMap. h.
# Ifndef _ SceneMap_H __# define _ SceneMap_H __# include "cocos2d. h "USING_NS_CC; class SceneMap: public Layer {public: // void update (float time); virtual bool init (); static Scene * scene (); CREATE_FUNC (SceneMap); TMXTiledMap * map1; TMXTiledMap * map2 ;}; # endif
Add the following code to SceneMap. cpp:
# Include "SceneMap. h "Scene * SceneMap: scene () {Scene * scene = Scene: create (); SceneMap * layer = SceneMap: create (); scene-> addChild (layer); return scene;} bool SceneMap: init () {if (! Layer: init () {return false;} Size winSize = Director: getInstance ()-> getWinSize (); // load map map1 = TMXTiledMap :: create ("map1.tmx"); map2 = TMXTiledMap: create ("map2.tmx"); addChild (map1); addChild (map2 ); // set the location of the second map to map2-> setPositionX (map1-> getPositionX () + map1-> getContentSize (). width); // start frame cyclic scheduling scheduleUpdate (); return true;} void SceneMap: update (float time) {// move each frame of map to the left five pixels map1-> setPositionX (map1-> getPositionX ()-5); map2-> setPositionX (map2-> getPositionX () -5); // if (map1-> getPositionX () + map1-> getContentSize (). width <= 0) {// Add the first map to the back of the second map // The horizontal coordinates of the first map = the horizontal coordinates of the second map + the width of the second map map1-> setPositionX (map2-> getPositionX () + map2-> getContentSize (). width );} // when the rightmost side of the second map and the leftmost side of the window are duplicated // Add the second map to the back of the first map if (map2-> getPositionX () + map2-> getContentSize (). width <= 0) {// Add the second map to the back of the first map // The horizontal coordinates of the second map = the horizontal coordinates of the first map + the width of the first map map2-> setPositionX (map1-> getPositionX () + map1-> getContentSize (). width );}}