cocod2d-x 之 CCTMXTiledMap & CCTMXLayer
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)
#ifndef __CCTMX_TILE_MAP_H__
#define __CCTMX_TILE_MAP_H__ #include "base_nodes/CCNode.h"
#include "CCTMXObjectGroup.h" NS_CC_BEGIN class CCTMXObjectGroup;
class CCTMXLayer;
class CCTMXLayerInfo;
class CCTMXTilesetInfo;
class CCTMXMapInfo; /** TMX地图类型 */
enum
{
/** 平面地图 */
CCTMXOrientationOrtho, /** 六角地图 */
CCTMXOrientationHex, /** 三维地图(45度斜视) */
CCTMXOrientationIso,
}; class CC_DLL CCTMXTiledMap : public CCNode
{
/** 地图尺寸(以瓦片为单位) */
CC_SYNTHESIZE_PASS_BY_REF(CCSize, m_tMapSize, MapSize); /** 地图尺寸(以像素为单位) */
CC_SYNTHESIZE_PASS_BY_REF(CCSize, m_tTileSize, TileSize); /** 地图类型(朝向) */
CC_SYNTHESIZE(int, m_nMapOrientation, MapOrientation); /** 地图内对象集合 */
CC_PROPERTY(CCArray*, m_pObjectGroups, ObjectGroups); /** 地图内对象字典 */
CC_PROPERTY(CCDictionary*, m_pProperties, Properties);
public: CCTMXTiledMap(); virtual ~CCTMXTiledMap(); /** 根据tmx文件创建地图 */
static CCTMXTiledMap* create(const char *tmxFile); /** 根据xml字符串和资源路径创建地图 */
static CCTMXTiledMap* createWithXML(const char* tmxString, const char* resourcePath); /** 根据tmx文件初始化地图 */
bool initWithTMXFile(const char *tmxFile); /** 根据xml字符串和资源路径初始化地图 */
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); private:
CCTMXLayer * parseLayer(CCTMXLayerInfo *layerInfo, CCTMXMapInfo *mapInfo);
CCTMXTilesetInfo * tilesetForLayer(CCTMXLayerInfo *layerInfo, CCTMXMapInfo *mapInfo);
void buildWithMapInfo(CCTMXMapInfo* mapInfo);
protected:
//地图属性字典
CCDictionary* m_pTileProperties; }; NS_CC_END
CCTMXTiledMap.h
#ifndef __CCTMX_LAYER_H__
#define __CCTMX_LAYER_H__ #include "CCTMXObjectGroup.h"
#include "base_nodes/CCAtlasNode.h"
#include "sprite_nodes/CCSpriteBatchNode.h"
#include "CCTMXXMLParser.h"
NS_CC_BEGIN class CCTMXMapInfo;
class CCTMXLayerInfo;
class CCTMXTilesetInfo;
struct _ccCArray; class CC_DLL CCTMXLayer : public CCSpriteBatchNode
{
/** layer尺寸( 以tile为单位) */
CC_SYNTHESIZE_PASS_BY_REF(CCSize, m_tLayerSize, LayerSize);
/** size of the map's tile (could be different from the tile's size) */
/** layer尺寸( 以tile为单位,可能与tile不同) */
CC_SYNTHESIZE_PASS_BY_REF(CCSize, m_tMapTileSize, MapTileSize);
/** pointer to the map of tiles */
CC_SYNTHESIZE(unsigned int*, m_pTiles, Tiles);
/** Tileset information for the layer */
CC_PROPERTY(CCTMXTilesetInfo*, m_pTileSet, TileSet);
/** Layer orientation, which is the same as the map orientation */
CC_SYNTHESIZE(unsigned int, m_uLayerOrientation, LayerOrientation);
/** properties from the layer. They can be added using Tiled */
CC_PROPERTY(CCDictionary*, m_pProperties, Properties);
public:
/**
* @js ctor
* @lua NA
*/
CCTMXLayer();
/**
* @js NA
* @lua NA
*/
virtual ~CCTMXLayer(); /** creates a CCTMXLayer with an tileset info, a layer info and a map info */
static CCTMXLayer * create(CCTMXTilesetInfo *tilesetInfo, CCTMXLayerInfo *layerInfo, CCTMXMapInfo *mapInfo); /** initializes a CCTMXLayer with a tileset info, a layer info and a map info
* @lua NA
*/
bool initWithTilesetInfo(CCTMXTilesetInfo *tilesetInfo, CCTMXLayerInfo *layerInfo, CCTMXMapInfo *mapInfo); /** dealloc the map that contains the tile position from memory.
Unless you want to know at runtime the tiles positions, you can safely call this method.
If you are going to call layer->tileGIDAt() then, don't release the map
*/
void releaseMap(); /** returns the tile (CCSprite) at a given a tile coordinate.
The returned CCSprite will be already added to the CCTMXLayer. Don't add it again.
The CCSprite can be treated like any other CCSprite: rotated, scaled, translated, opacity, color, etc.
You can remove either by calling:
- layer->removeChild(sprite, cleanup);
- or layer->removeTileAt(ccp(x,y));
@js getTileGIDAt
*/
CCSprite* tileAt(const CCPoint& tileCoordinate); /** returns the tile gid at a given tile coordinate.
if it returns 0, it means that the tile is empty.
This method requires the the tile map has not been previously released (eg. don't call layer->releaseMap())
@js tileGIDAt
*/
unsigned int tileGIDAt(const CCPoint& tileCoordinate); /** returns the tile gid at a given tile coordinate. It also returns the tile flags.
This method requires the the tile map has not been previously released (eg. don't call [layer releaseMap])
@js tileGIDAt
@lua NA
*/
unsigned int tileGIDAt(const CCPoint& tileCoordinate, ccTMXTileFlags* flags); /** sets the tile gid (gid = tile global id) at a given tile coordinate.
The Tile GID can be obtained by using the method "tileGIDAt" or by using the TMX editor -> Tileset Mgr +1.
If a tile is already placed at that position, then it will be removed.
*/
void setTileGID(unsigned int gid, const CCPoint& tileCoordinate); /** sets the tile gid (gid = tile global id) at a given tile coordinate.
The Tile GID can be obtained by using the method "tileGIDAt" or by using the TMX editor -> Tileset Mgr +1.
If a tile is already placed at that position, then it will be removed. Use withFlags if the tile flags need to be changed as well
*/ void setTileGID(unsigned int gid, const CCPoint& tileCoordinate, ccTMXTileFlags flags); /** removes a tile at given tile coordinate */
void removeTileAt(const CCPoint& tileCoordinate); /** returns the position in points of a given tile coordinate
* @js getPositionAt
*/
CCPoint positionAt(const CCPoint& tileCoordinate); /** return the value for the specific property name
* @js getProperty
*/
CCString *propertyNamed(const char *propertyName); /** Creates the tiles */
void setupTiles(); /** CCTMXLayer doesn't support adding a CCSprite manually.
* @warning addchild(z, tag); is not supported on CCTMXLayer. Instead of setTileGID.
* @lua NA
*/
virtual void addChild(CCNode * child, int zOrder, int tag);
/** super method
* @lua NA
*/
void removeChild(CCNode* child, bool cleanup); inline const char* getLayerName(){ return m_sLayerName.c_str(); }
inline void setLayerName(const char *layerName){ m_sLayerName = layerName; }
private:
CCPoint positionForIsoAt(const CCPoint& pos);
CCPoint positionForOrthoAt(const CCPoint& pos);
CCPoint positionForHexAt(const CCPoint& pos); CCPoint calculateLayerOffset(const CCPoint& offset); /* optimization methods */
CCSprite* appendTileForGID(unsigned int gid, const CCPoint& pos);
CCSprite* insertTileForGID(unsigned int gid, const CCPoint& pos);
CCSprite* updateTileForGID(unsigned int gid, const CCPoint& pos); /* The layer recognizes some special properties, like cc_vertez */
void parseInternalProperties();
void setupTileSprite(CCSprite* sprite, CCPoint pos, unsigned int gid);
CCSprite* reusedTileWithRect(CCRect rect);
int vertexZForPos(const CCPoint& pos); // index
unsigned int atlasIndexForExistantZ(unsigned int z);
unsigned int atlasIndexForNewZ(int z);
protected:
// layer的名称
std::string m_sLayerName;
//TMX Layer 的透明度
unsigned char m_cOpacity; unsigned int m_uMinGID;
unsigned int m_uMaxGID; //vertexZ 启用后该参数才有效
int m_nVertexZvalue;
bool m_bUseAutomaticVertexZ; // 用于优化
CCSprite *m_pReusedTile;
ccCArray *m_pAtlasIndexArray; // 用于视网膜显示屏
float m_fContentScaleFactor;
}; NS_CC_END #endif //__CCTMX_LAYER_H__
CCTMXLayer.h
cocod2d-x 之 CCTMXTiledMap & CCTMXLayer的更多相关文章
- cocos2dx进阶学习之CCTMXTiledMap
继承关系 CCTMXTiledMap -> CCNode 它由CCNode派生,我们已经知道CCNode是cocos2dx的舞台对象的公共父类,所以CCTMXTiledMap也是个舞台对象 成员 ...
- cocos2dx进阶学习之CCTMXLayer
继承关系 CCTMXLayer -> CCSpriteBatchNode CCTMXLayer是在瓦片地图中,抽象一个层的类,它继承自CCSpriteBatchNode,也就是说它抽象了一批相同 ...
- [原创]cocos2d-x研习录-第三阶 特性之瓦片地图集
由于一张大的世界地图或背景图片往往可以由屈指可数的几种地形来表示,每种地形对应于一张小的图片,我们称这些小的地形图片为瓦片.把这些瓦片拼接在一起,组合成一个完整的地图,这就是瓦片地图集的基本原理. C ...
- cocos2dx中的格子地图TileMap
格子地图的优点: a.节省内存,我们知道对于一款游戏来说,如果以图片来作为地图的话,对于神庙逃亡,魂斗罗这样的场景很多,地图很长的游戏显然不现实,因为图片很占内存,但是这些游戏的地图有一个特点就是:重 ...
- cocos2d-x 小技巧
1.字符串 与 数据结构互转 CCPoint: CCPointFromString(); {x, y} CCSize: CCSizeFromString(); {w, h} CCRect: CCSiz ...
- cocos2d-x学习笔记
转自:http://blog.csdn.net/we000636/article/details/8263503 接受触屏事件的优先级是值越小,响应触屏事件的优先级越高 Z值越大,越外面 JNI:允许 ...
- [置顶] COcos2d-X 中文API
本文来自http://blog.csdn.net/runaying ,引用必须注明出处! COcos2d-X 中文API 温馨提醒:使用二维码扫描软件,就可以在手机上访问我的博客啦!另外大家可以访问另 ...
- cocos2d-x中的Tiled地图
cocos2d-x中的瓦片地图是通过tiledMap软件制作的,存档格式是.tmx格式.此软件的使用步骤简单总结如下: (1)制作瓦片地图 1 打开软件,软件界面如下图. 2. 新建地图(文件-> ...
- cocos2d-x游戏开发系列教程-超级玛丽07-CMGameMap
背景 在上一篇博客中,我们提到CMGameScene,但是CMGameScene只是个框架,实际担任游戏逻辑的是CMGameMap类,这个博文就来了解下CMGameMap 头文件 class CMGa ...
随机推荐
- Ubuntu学习笔记-win7&Ubuntu双系统简单搭建系统指南
win7&Ubuntu双系统简单搭建系统指南 本文是自己老本子折腾Ubuntu的一些记录,主要是搭建了一个能够足够娱乐(不玩游戏)专注练习自己编程能力的内容.只是简单的写了关于系统的安装和一些 ...
- vijosP1059 积木城堡
vijosP1059 积木城堡 链接:https://vijos.org/p/1059 [思路] 01背包. 刚开始想麻烦了,想的是二分答案然后01背包判断是否可行,但是首先答案不满足单调性所以不能二 ...
- 折腾iPhone的生活——通过设置使iPhone更省电
入手了iPhone5s,上手感觉iPhone没有过去省电了,可能是iOS7的关系,也有可能是我一直在下应用的关系,但是iPhone5s那1500mA的电池的确是有点真的不那么够用的样子,通过设置来省电 ...
- Ural 1046 Geometrical Dreams(解方程+计算几何)
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1046 参考博客:http://hi.baidu.com/cloudygoose/item ...
- Little shop of flowers - SGU 104 (DP)
题目大意:把 M 朵花插入 N 个花瓶中,每个花插入不同的花瓶都有一个价值A[Mi][Nj],要使所有的花都插入花瓶,求出来最大的总价值(花瓶为空时价值是0). 分析:dp[i][j]表示前i朵花插入 ...
- UVa11526 H(n)
http://blog.csdn.net/synapse7/article/details/12873437 #include<cstdio> #include<cstring> ...
- 执行游戏时出现0xc000007b错误的解决方法
如图,这个错误使无数玩家烦恼. 出现这个错误,可能是硬件的问题,也可能是软件的问题.可是,因为硬件引起该问题的概率非常小,而且除了更换硬件之外没有更好的解决方法,因此本文将具体介绍怎样通过软件解决此问 ...
- Apache让一台虚拟主机接受多域名解析(转)
之前写了一篇文章关于linux下apache虚拟主机配置,配置那是相当简单: <VirtualHost *:80> ServerAdmin admin@example.com Docume ...
- Android中悬浮窗口的实现原理和示例代码
用了我一个周末的时间,个中愤懑就不说了,就这个问题,我翻遍全球网络没有一篇像样的资料,现在将实现原理简单叙述如下: 调用WindowManager,并设置WindowManager.LayoutPar ...
- js鼠标事件
今天遇到一个非常奇怪而又搞笑的事情:给一个a标签添加一个鼠标移动上时给一个事件,我给其添加的是一个onMouseMove事件,结果在IE6 7 8 9和GOOLE中都很正常,结果在Firox中出现问题 ...