地图转动

参考:http://blog.csdn.net/u010105970/article/details/45288017

map1、map2分别为两幅地图;

首先创建一个SceneMap类,在SceneMap.h中添加下面的代码

[cpp]  view plain copy
  1. #ifndef _SceneMap_H__  
  2. #define _SceneMap_H__  
  3.   
  4. #include "cocos2d.h"  
  5. USING_NS_CC;  
  6.   
  7. class SceneMap : public Layer  
  8. {  
  9. public:  
  10.   
  11.     //帧循环调度函数  
  12.     void update(float time);  
  13.   
  14.     virtual bool init();    
  15.     static Scene* scene();  
  16.     CREATE_FUNC(SceneMap);  
  17.   
  18.     TMXTiledMap* map1;  
  19.     TMXTiledMap* map2;  
  20. };  
  21.   
  22. #endif  

 

在SceneMap.cpp中添加下面的代码

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include "SceneMap.h"  
  2.   
  3.   
  4. Scene* SceneMap::scene()  
  5. {  
  6.     Scene *scene = Scene::create();  
  7.   
  8.     SceneMap *layer = SceneMap::create();  
  9.       
  10.     scene->addChild(layer);  
  11.       
  12.     return scene;  
  13. }  
  14.   
  15.   
  16. bool SceneMap::init()  
  17. {  
  18.     if(!Layer::init())  
  19.     {  
  20.         return false;  
  21.     }  
  22.   
  23.     Size winSize = Director::getInstance()->getWinSize();  
  24.   
  25.     //加载地图  
  26.     map1 = TMXTiledMap::create("map1.tmx");  
  27.     map2 = TMXTiledMap::create("map2.tmx");  
  28.   
  29.     addChild(map1);  
  30.     addChild(map2);  
  31.   
  32.     //将第二张地图的位置设置到第二张地图的后面  
  33.     map2->setPositionX(map1->getPositionX() + map1->getContentSize().width);  
  34.   
  35.     //启动帧循环调度  
  36.     scheduleUpdate();  
  37.       
  38.     return true;  
  39. }  
  40.   
  41.   
  42. void SceneMap::update(float time)  
  43. {  
  44.     //每一帧地图向左移动5个像素  
  45.     map1->setPositionX(map1->getPositionX() - 5);  
  46.     map2->setPositionX(map2->getPositionX() - 5);  
  47.   
  48.     //当第一张地图的最右端和窗口的最左端重合时  
  49.     if(map1->getPositionX() + map1->getContentSize().width <= 0)  
  50.     {  
  51.         //将第一张地图添加到第二张地图后面  
  52.         //第一张地图的横坐标 = 第二张地图的横坐标 + 第二张地图的宽度  
  53.         map1->setPositionX(map2->getPositionX() + map2->getContentSize().width);  
  54.     }  
  55.   
  56.   
  57.     //当第二张地图的最右端和窗口的最左端重合时  
  58.     //将第二张地图添加到第一张地图后面  
  59.     if(map2->getPositionX() + map2->getContentSize().width <= 0)  
  60.     {  
  61.         //将第二张地图添加到第一张地图后面  
  62.         //第二张地图的横坐标 = 第一张地图的横坐标 + 第一张地图的宽度  
  63.         map2->setPositionX(map1->getPositionX() + map1->getContentSize().width);  
  64.     }  
  65. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值