参考:http://blog.csdn.net/u010105970/article/details/45288017
map1、map2分别为两幅地图;
首先创建一个SceneMap类,在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
在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();
- //加载地图
- map1 = TMXTiledMap::create("map1.tmx");
- map2 = TMXTiledMap::create("map2.tmx");
- addChild(map1);
- addChild(map2);
- //将第二张地图的位置设置到第二张地图的后面
- map2->setPositionX(map1->getPositionX() + map1->getContentSize().width);
- //启动帧循环调度
- scheduleUpdate();
- return true;
- }
- void SceneMap::update(float time)
- {
- //每一帧地图向左移动5个像素
- map1->setPositionX(map1->getPositionX() - 5);
- map2->setPositionX(map2->getPositionX() - 5);
- //当第一张地图的最右端和窗口的最左端重合时
- if(map1->getPositionX() + map1->getContentSize().width <= 0)
- {
- //将第一张地图添加到第二张地图后面
- //第一张地图的横坐标 = 第二张地图的横坐标 + 第二张地图的宽度
- map1->setPositionX(map2->getPositionX() + map2->getContentSize().width);
- }
- //当第二张地图的最右端和窗口的最左端重合时
- //将第二张地图添加到第一张地图后面
- if(map2->getPositionX() + map2->getContentSize().width <= 0)
- {
- //将第二张地图添加到第一张地图后面
- //第二张地图的横坐标 = 第一张地图的横坐标 + 第一张地图的宽度
- map2->setPositionX(map1->getPositionX() + map1->getContentSize().width);
- }
- }
-