I recently do a parkour game, in the parkour game will use the unlimited scroll of the map, after looking at a lot of information on the Internet, I also understand the map infinite scrolling implementation method.
For a more graphic introduction to the infinite scrolling of the map, I purposely drew a few pictures
The first two maps need to be prepared, and the first map is placed on the window at the time of initialization, and the second map is placed behind the first map.
Scrolls the map when the right end of the first map coincides with the leftmost end of the window
Put the first map behind the second map
When the right end of the second map is at the leftmost end of the window
Place the second map behind the first map (the leftmost side of the first map coincides with the leftmost end of the second map)
Above is the implementation logic of the map infinite loop scrolling, the following code to achieve unlimited scrolling of the map
First create a Scenemap class and add the following code to the SceneMap.h
#ifndef _scenemap_h__#define _scenemap_h__#include "cocos2d.h" Using_ns_cc;class scenemap:public layer{public:// Frame loop schedule function void update (float time); virtual BOOL init (); Static scene* Scene (); Create_func (SCENEMAP); tmxtiledmap* Map1; Tmxtiledmap* map2;}; #endif
Add the following code in the 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 the back of the second map Map2->setpositionx ( Map1->getpositionx () + map1->getcontentsize (). width);//start frame loop scheduling scheduleupdate (); return true;} void Scenemap::update (float time) {//each frame of the map moves left 5 pixels map1->setpositionx (Map1->getpositionx ()-5);map2-> Setpositionx (Map2->getpositionx ()-5);//When the leftmost side of the first map coincides with the leftmost end of the window, if (Map1->getpositionx () + map1-> Getcontentsize (). Width <= 0) {//Add the first map to the back of the second map//The horizontal axis of the first map = the horizontal axis of the second map + the width of the second map Map1->setpositionx (map2-> Getpositionx () + map2->getcontentsize (). width);} When the right end of the second map coincides with the leftmost end of the window//adds 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 axis of the second map = the horizontal axis of the first map + the width of the first map Map2->setpositionx ( Map1->getpositionx () + map1->getcontentsize (). width);}}
cocos2d-x3.0 to achieve unlimited scrolling of the map