cocos2dx框架自带的地图CCTMXTiledMap,继承自CCNode。CCTMXTiledMap的坐标系的原点位于左上角,以一个瓦片为单位,换句话说,左上角第一块瓦片的坐标为(0,0),而紧挨着它的右边的瓦片坐标就是(1,0)。TileMap中的每一个瓦片拥有一个唯一的编号GID,用于在地图中查找某个瓦片。Cocos2d-x提供了一系列方法,可以从瓦片地图坐标获取对应瓦片的GID,同时还可以利用这些方法来判断某个坐标下是否存在瓦片。

属性:

CCSize  m_tMapSize,地图大小(以瓦片为单位)

CCSize  m_tTileSize,瓦片大小(以像素为单位)

int  m_nMapOrientation,地图类型(enum{CCTMXOrientationOrtho,CCTMXOrientationHex,CCTMXOrientationIso,};

CCArray*  m_pObjectGroups,对象集合

CCDictionary* m_pProperties,属性字典

CCDictionary* m_pTileProperties,瓦片属性

方法:

创建地图

static CCTMXTiledMap* create(const char *tmxFile)

static CCTMXTiledMap* createWithXML(const char* tmxString, const char* resourcePath)

地图初始化

bool initWithTMXFile(const char *tmxFile)

bool initWithXML(const char* tmxString, const char* resourcePath)

获取地图层

CCTMXLayer* layerNamed(const char *layerName)

获取根据名称对象组

CCTMXObjectGroup* objectGroupNamed(const char *groupName)

获取属性值

CCString *propertyNamed(const char *propertyName)

根据瓦片GID获取属性字典

CCDictionary* propertiesForGID(int GID)

  CCTMXLayer继承自CCSpriteBatchNode,代表一个瓦片地图中的图层,可以从图层对象获取图层信息,如某一点是否存在对象组或属性。CCTMXLayer坐标以地图层的左下角为原点(不是屏幕左下角),以像素为单位,当把地图层坐标pos转换为世界坐标时需要将pos的x坐标减去地图滚动的距离。当把CCTMXTiledMap坐标转换为CCTMXLayer坐标时,注意乘瓦片大小和原点转换。

属性:

std::string m_sLayerName,图层名称

unsigned char m_cOpacity,透明度

unsigned int m_uMinGID
unsigned int m_uMaxGID

int m_nVertexZvalue,Z轴
bool m_bUseAutomaticVertexZ,由框架自动管理Z轴值

float m_fContentScaleFactor,缩放

CCSize  m_tLayerSize,图层大小(以瓦片为单位)

CCSize  m_tMapTileSize,瓦片大小(可能与tileMap不同)

unsigned int*  m_pTiles,指向瓦片的指针

CCTMXTilesetInfo*  m_pTileSet,图层瓦片信息

unsigned int  m_uLayerOrientation,和tileMap一样

CCDictionary*  m_pProperties图层属性

方法

CCSprite* tileAt(const CCPoint& tileCoordinate),根据瓦片坐标返回瓦片精灵

unsigned int  tileGIDAt(const CCPoint& tileCoordinate),根据瓦片坐标返回GID,如为空则返回0

unsigned int tileGIDAt(const CCPoint& tileCoordinate, ccTMXTileFlags* flags),同上且返回flags

void setTileGID(unsigned int gid, const CCPoint& tileCoordinate),设置GID

void setTileGID(unsigned int gid, const CCPoint& tileCoordinate, ccTMXTileFlags flags)

void removeTileAt(const CCPoint& tileCoordinate),删除瓦片

CCPoint positionAt(const CCPoint& tileCoordinate),返回像素坐标

CCString *propertyNamed(const char *propertyName),获取属性

virtual void addChild(CCNode * child, int zOrder, int tag),添加对象

void removeChild(CCNode* child, bool cleanup),删除对象

const char* getLayerName()

void setLayerName(const char *layerName)

  1. #ifndef __CCTMX_TILE_MAP_H__
  2. #define __CCTMX_TILE_MAP_H__
  3.  
  4. #include "base_nodes/CCNode.h"
  5. #include "CCTMXObjectGroup.h"
  6.  
  7. NS_CC_BEGIN
  8.  
  9. class CCTMXObjectGroup;
  10. class CCTMXLayer;
  11. class CCTMXLayerInfo;
  12. class CCTMXTilesetInfo;
  13. class CCTMXMapInfo;
  14.  
  15. /** TMX地图类型 */
  16. enum
  17. {
  18. /** 平面地图 */
  19. CCTMXOrientationOrtho,
  20.  
  21. /** 六角地图 */
  22. CCTMXOrientationHex,
  23.  
  24. /** 三维地图(45度斜视) */
  25. CCTMXOrientationIso,
  26. };
  27.  
  28. class CC_DLL CCTMXTiledMap : public CCNode
  29. {
  30. /** 地图尺寸(以瓦片为单位) */
  31. CC_SYNTHESIZE_PASS_BY_REF(CCSize, m_tMapSize, MapSize);
  32.  
  33. /** 地图尺寸(以像素为单位) */
  34. CC_SYNTHESIZE_PASS_BY_REF(CCSize, m_tTileSize, TileSize);
  35.  
  36. /** 地图类型(朝向) */
  37. CC_SYNTHESIZE(int, m_nMapOrientation, MapOrientation);
  38.  
  39. /** 地图内对象集合 */
  40. CC_PROPERTY(CCArray*, m_pObjectGroups, ObjectGroups);
  41.  
  42. /** 地图内对象字典 */
  43. CC_PROPERTY(CCDictionary*, m_pProperties, Properties);
  44. public:
  45.  
  46. CCTMXTiledMap();
  47.  
  48. virtual ~CCTMXTiledMap();
  49.  
  50. /** 根据tmx文件创建地图 */
  51. static CCTMXTiledMap* create(const char *tmxFile);
  52.  
  53. /** 根据xml字符串和资源路径创建地图 */
  54. static CCTMXTiledMap* createWithXML(const char* tmxString, const char* resourcePath);
  55.  
  56. /** 根据tmx文件初始化地图 */
  57. bool initWithTMXFile(const char *tmxFile);
  58.  
  59. /** 根据xml字符串和资源路径初始化地图 */
  60. bool initWithXML(const char* tmxString, const char* resourcePath);
  61.  
  62. /** 获取地图中的层 */
  63. CCTMXLayer* layerNamed(const char *layerName);
  64.  
  65. /** 获取对象集合 */
  66. CCTMXObjectGroup* objectGroupNamed(const char *groupName);
  67.  
  68. /** 获取属性值 */
  69. CCString *propertyNamed(const char *propertyName);
  70.  
  71. /** 根据GID返回属性字典 */
  72. CCDictionary* propertiesForGID(int GID);
  73.  
  74. private:
  75. CCTMXLayer * parseLayer(CCTMXLayerInfo *layerInfo, CCTMXMapInfo *mapInfo);
  76. CCTMXTilesetInfo * tilesetForLayer(CCTMXLayerInfo *layerInfo, CCTMXMapInfo *mapInfo);
  77. void buildWithMapInfo(CCTMXMapInfo* mapInfo);
  78. protected:
  79. //地图属性字典
  80. CCDictionary* m_pTileProperties;
  81.  
  82. };
  83.  
  84. NS_CC_END

CCTMXTiledMap.h

  1. #ifndef __CCTMX_LAYER_H__
  2. #define __CCTMX_LAYER_H__
  3.  
  4. #include "CCTMXObjectGroup.h"
  5. #include "base_nodes/CCAtlasNode.h"
  6. #include "sprite_nodes/CCSpriteBatchNode.h"
  7. #include "CCTMXXMLParser.h"
  8. NS_CC_BEGIN
  9.  
  10. class CCTMXMapInfo;
  11. class CCTMXLayerInfo;
  12. class CCTMXTilesetInfo;
  13. struct _ccCArray;
  14.  
  15. class CC_DLL CCTMXLayer : public CCSpriteBatchNode
  16. {
  17. /** layer尺寸( 以tile为单位) */
  18. CC_SYNTHESIZE_PASS_BY_REF(CCSize, m_tLayerSize, LayerSize);
  19. /** size of the map's tile (could be different from the tile's size) */
  20. /** layer尺寸( 以tile为单位,可能与tile不同) */
  21. CC_SYNTHESIZE_PASS_BY_REF(CCSize, m_tMapTileSize, MapTileSize);
  22. /** pointer to the map of tiles */
  23. CC_SYNTHESIZE(unsigned int*, m_pTiles, Tiles);
  24. /** Tileset information for the layer */
  25. CC_PROPERTY(CCTMXTilesetInfo*, m_pTileSet, TileSet);
  26. /** Layer orientation, which is the same as the map orientation */
  27. CC_SYNTHESIZE(unsigned int, m_uLayerOrientation, LayerOrientation);
  28. /** properties from the layer. They can be added using Tiled */
  29. CC_PROPERTY(CCDictionary*, m_pProperties, Properties);
  30. public:
  31. /**
  32. * @js ctor
  33. * @lua NA
  34. */
  35. CCTMXLayer();
  36. /**
  37. * @js NA
  38. * @lua NA
  39. */
  40. virtual ~CCTMXLayer();
  41.  
  42. /** creates a CCTMXLayer with an tileset info, a layer info and a map info */
  43. static CCTMXLayer * create(CCTMXTilesetInfo *tilesetInfo, CCTMXLayerInfo *layerInfo, CCTMXMapInfo *mapInfo);
  44.  
  45. /** initializes a CCTMXLayer with a tileset info, a layer info and a map info
  46. * @lua NA
  47. */
  48. bool initWithTilesetInfo(CCTMXTilesetInfo *tilesetInfo, CCTMXLayerInfo *layerInfo, CCTMXMapInfo *mapInfo);
  49.  
  50. /** dealloc the map that contains the tile position from memory.
  51. Unless you want to know at runtime the tiles positions, you can safely call this method.
  52. If you are going to call layer->tileGIDAt() then, don't release the map
  53. */
  54. void releaseMap();
  55.  
  56. /** returns the tile (CCSprite) at a given a tile coordinate.
  57. The returned CCSprite will be already added to the CCTMXLayer. Don't add it again.
  58. The CCSprite can be treated like any other CCSprite: rotated, scaled, translated, opacity, color, etc.
  59. You can remove either by calling:
  60. - layer->removeChild(sprite, cleanup);
  61. - or layer->removeTileAt(ccp(x,y));
  62. @js getTileGIDAt
  63. */
  64. CCSprite* tileAt(const CCPoint& tileCoordinate);
  65.  
  66. /** returns the tile gid at a given tile coordinate.
  67. if it returns 0, it means that the tile is empty.
  68. This method requires the the tile map has not been previously released (eg. don't call layer->releaseMap())
  69. @js tileGIDAt
  70. */
  71. unsigned int tileGIDAt(const CCPoint& tileCoordinate);
  72.  
  73. /** returns the tile gid at a given tile coordinate. It also returns the tile flags.
  74. This method requires the the tile map has not been previously released (eg. don't call [layer releaseMap])
  75. @js tileGIDAt
  76. @lua NA
  77. */
  78. unsigned int tileGIDAt(const CCPoint& tileCoordinate, ccTMXTileFlags* flags);
  79.  
  80. /** sets the tile gid (gid = tile global id) at a given tile coordinate.
  81. The Tile GID can be obtained by using the method "tileGIDAt" or by using the TMX editor -> Tileset Mgr +1.
  82. If a tile is already placed at that position, then it will be removed.
  83. */
  84. void setTileGID(unsigned int gid, const CCPoint& tileCoordinate);
  85.  
  86. /** sets the tile gid (gid = tile global id) at a given tile coordinate.
  87. The Tile GID can be obtained by using the method "tileGIDAt" or by using the TMX editor -> Tileset Mgr +1.
  88. If a tile is already placed at that position, then it will be removed.
  89.  
  90. Use withFlags if the tile flags need to be changed as well
  91. */
  92.  
  93. void setTileGID(unsigned int gid, const CCPoint& tileCoordinate, ccTMXTileFlags flags);
  94.  
  95. /** removes a tile at given tile coordinate */
  96. void removeTileAt(const CCPoint& tileCoordinate);
  97.  
  98. /** returns the position in points of a given tile coordinate
  99. * @js getPositionAt
  100. */
  101. CCPoint positionAt(const CCPoint& tileCoordinate);
  102.  
  103. /** return the value for the specific property name
  104. * @js getProperty
  105. */
  106. CCString *propertyNamed(const char *propertyName);
  107.  
  108. /** Creates the tiles */
  109. void setupTiles();
  110.  
  111. /** CCTMXLayer doesn't support adding a CCSprite manually.
  112. * @warning addchild(z, tag); is not supported on CCTMXLayer. Instead of setTileGID.
  113. * @lua NA
  114. */
  115. virtual void addChild(CCNode * child, int zOrder, int tag);
  116. /** super method
  117. * @lua NA
  118. */
  119. void removeChild(CCNode* child, bool cleanup);
  120.  
  121. inline const char* getLayerName(){ return m_sLayerName.c_str(); }
  122. inline void setLayerName(const char *layerName){ m_sLayerName = layerName; }
  123. private:
  124. CCPoint positionForIsoAt(const CCPoint& pos);
  125. CCPoint positionForOrthoAt(const CCPoint& pos);
  126. CCPoint positionForHexAt(const CCPoint& pos);
  127.  
  128. CCPoint calculateLayerOffset(const CCPoint& offset);
  129.  
  130. /* optimization methods */
  131. CCSprite* appendTileForGID(unsigned int gid, const CCPoint& pos);
  132. CCSprite* insertTileForGID(unsigned int gid, const CCPoint& pos);
  133. CCSprite* updateTileForGID(unsigned int gid, const CCPoint& pos);
  134.  
  135. /* The layer recognizes some special properties, like cc_vertez */
  136. void parseInternalProperties();
  137. void setupTileSprite(CCSprite* sprite, CCPoint pos, unsigned int gid);
  138. CCSprite* reusedTileWithRect(CCRect rect);
  139. int vertexZForPos(const CCPoint& pos);
  140.  
  141. // index
  142. unsigned int atlasIndexForExistantZ(unsigned int z);
  143. unsigned int atlasIndexForNewZ(int z);
  144. protected:
  145. // layer的名称
  146. std::string m_sLayerName;
  147. //TMX Layer 的透明度
  148. unsigned char m_cOpacity;
  149.  
  150. unsigned int m_uMinGID;
  151. unsigned int m_uMaxGID;
  152.  
  153. //vertexZ 启用后该参数才有效
  154. int m_nVertexZvalue;
  155. bool m_bUseAutomaticVertexZ;
  156.  
  157. // 用于优化
  158. CCSprite *m_pReusedTile;
  159. ccCArray *m_pAtlasIndexArray;
  160.  
  161. // 用于视网膜显示屏
  162. float m_fContentScaleFactor;
  163. };
  164.  
  165. NS_CC_END
  166.  
  167. #endif //__CCTMX_LAYER_H__

CCTMXLayer.h

cocod2d-x 之 CCTMXTiledMap & CCTMXLayer的更多相关文章

  1. cocos2dx进阶学习之CCTMXTiledMap

    继承关系 CCTMXTiledMap -> CCNode 它由CCNode派生,我们已经知道CCNode是cocos2dx的舞台对象的公共父类,所以CCTMXTiledMap也是个舞台对象 成员 ...

  2. cocos2dx进阶学习之CCTMXLayer

    继承关系 CCTMXLayer -> CCSpriteBatchNode CCTMXLayer是在瓦片地图中,抽象一个层的类,它继承自CCSpriteBatchNode,也就是说它抽象了一批相同 ...

  3. [原创]cocos2d-x研习录-第三阶 特性之瓦片地图集

    由于一张大的世界地图或背景图片往往可以由屈指可数的几种地形来表示,每种地形对应于一张小的图片,我们称这些小的地形图片为瓦片.把这些瓦片拼接在一起,组合成一个完整的地图,这就是瓦片地图集的基本原理. C ...

  4. cocos2dx中的格子地图TileMap

    格子地图的优点: a.节省内存,我们知道对于一款游戏来说,如果以图片来作为地图的话,对于神庙逃亡,魂斗罗这样的场景很多,地图很长的游戏显然不现实,因为图片很占内存,但是这些游戏的地图有一个特点就是:重 ...

  5. cocos2d-x 小技巧

    1.字符串 与 数据结构互转 CCPoint: CCPointFromString(); {x, y} CCSize: CCSizeFromString(); {w, h} CCRect: CCSiz ...

  6. cocos2d-x学习笔记

    转自:http://blog.csdn.net/we000636/article/details/8263503 接受触屏事件的优先级是值越小,响应触屏事件的优先级越高 Z值越大,越外面 JNI:允许 ...

  7. [置顶] COcos2d-X 中文API

    本文来自http://blog.csdn.net/runaying ,引用必须注明出处! COcos2d-X 中文API 温馨提醒:使用二维码扫描软件,就可以在手机上访问我的博客啦!另外大家可以访问另 ...

  8. cocos2d-x中的Tiled地图

    cocos2d-x中的瓦片地图是通过tiledMap软件制作的,存档格式是.tmx格式.此软件的使用步骤简单总结如下: (1)制作瓦片地图 1 打开软件,软件界面如下图. 2. 新建地图(文件-> ...

  9. cocos2d-x游戏开发系列教程-超级玛丽07-CMGameMap

    背景 在上一篇博客中,我们提到CMGameScene,但是CMGameScene只是个框架,实际担任游戏逻辑的是CMGameMap类,这个博文就来了解下CMGameMap 头文件 class CMGa ...

随机推荐

  1. ANDROID Porting系列二、配置一个新产品

    ANDROID Porting系列二.配置一个新产品 详细说明 下面的步骤描述了如何配置新的移动设备和产品的makefile运行android. 1.         目录//vendor/创建一个公 ...

  2. HDU 4414 Finding crosses (DFS + BFS)

    题意:在N*N的图中,找出孤立存在的十字架的个数.十字架要求为正十字,孤立表示组成十字架的‘#的周围的一格再无’#‘. dfs找出在中心的‘#’(周围四格也为‘#'),则缩小了搜索范围,再bfs找出是 ...

  3. CMDLINE的解析

    在linux的config文件中有一个特殊的宏定义CMDLINE,以前也一直在使用这个宏的参数,但是真正这个宏的解析和使用却不怎么明确.这次有机会多对它有些了解,不妨把这个浅显的认识说出来,记下来. ...

  4. winform 菜单项显示历史记录 分类: WinForm 2014-07-11 18:15 196人阅读 评论(0) 收藏

    (1)创建一个项目,将其命名为MenuHistory,默认窗体为Form1. (2)从工具箱中向Form1窗体添加MenuStrip控件,同时向窗体添加OpenFileDialog控件.创建一个&qu ...

  5. Websense更名换帅

    Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE /* Style Definitions */ table.MsoNormalTable ...

  6. (转)百度Map API

    转自  http://blog.sina.com.cn/s/blog_6079f38301013sb3.html 一.与地图操作相关的接口哦! (这些接口的开启都是写在执行成功的回调函数那里) map ...

  7. cocos2d-x 3.0 final 中文显示

    cocos2d-x 3.0的中文显示非常easy,首先,你须要一个xml文件保存中文,还须要一个能显示中文的TTF文件 <?xml version="1.0" encodin ...

  8. JavaCodeTra 猴子选猴王 约瑟夫循环

    之前用的是循环链表,java刚学,不知道怎么用链表.用个小算法吧 代码: import java.util.Scanner; /** * */ /** * @author john * @约瑟夫循环/ ...

  9. eclipse 上安装systemgui

    http://wiki.eclipse.org/Linux_Tools_Project/PluginInstallHelp http://wiki.eclipse.org/Linux_Tools_Pr ...

  10. 利用 SysRq 键排除和诊断系统故障

    说白了,SysRq手动触发而不用命令, /proc/sysrq-trigger 这个是通过命令接口 实验:LINUX窗口下 ,CTRL+ALT+F1,切换到TTY1,在文本模式下,按下 ALT+Sys ...