Cocos2d-X3.0 achieves unlimited rolling of Map

Source: Internet
Author: User

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 );}}

 

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.