1. tiledmap
TMX: Only 1 tileset per layer is supported
多个图层可以来自于一个图块,一个图层只能来自于一个图块。
Tiled软件没有这个限制,如果需要让cocos2dx能够支持一个图层有多个图块,需要自己修改。
我的方案是不修改cocos2dx的这个限制,而是同一图层的不同图块根据图块名字自动新建图层,把不属于该层的瓦片删掉,并放在其对应新图层上。
加载完毕之后再自行调整zorder。
有时间再研究让它支持一个图层支持多个图块
position convert to tile index
Isometric(等距地图)
可以从CCPoint CCTMXLayer::positionForIsoAt(const CCPoint& pos)函数中看到从瓦片坐标到实际坐标的转换
容易发现瓦片的坐标点是菱形瓦片左边的顶点
解这个xy的方程可以得到一个大概的从实际坐标转换到瓦片坐标的公式
这个反向转换不够精确
int h = (_tileMap->getMapSize().height * 2 + _tileMap->getMapSize().width -3)/2.0 - position.x/_tileMap->getTileSize().width - position.y/_tileMap->getTileSize().height;
int w = (_tileMap->getMapSize().height * 2 - _tileMap->getMapSize().width - 1)/2.0 + position.x/_tileMap->getTileSize().width - position.y/_tileMap->getTileSize().height;
return CCPoint(w, h);
support tile offset
cocos2dx 2.1.5不支持瓦片地图图块的偏移,可以稍作修改来支持他
class CC_DLL CCTMXTilesetInfo : public CCObject
{
public:
std::string m_sName;
unsigned int m_uFirstGid;
CCSize m_tTileSize;
CCSize m_tTileOffset;
...
};
CCTMXTilesetInfo::CCTMXTilesetInfo()
:m_uFirstGid(0)
,m_tTileSize(CCSizeZero)
, m_tTileOffset(CCSizeZero)
...
{
}
class CC_DLL CCTMXTilesetInfo : public CCObject
{
public:
std::string m_sName;
unsigned int m_uFirstGid;
CCSize m_tTileSize;
CCSize m_tTileOffset;
...
};
CCTMXTilesetInfo::CCTMXTilesetInfo()
:m_uFirstGid(0)
,m_tTileSize(CCSizeZero)
, m_tTileOffset(CCSizeZero)
...
{
}
void CCTMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
{
...
else if (elementName == "tileoffset")
{
CCTMXTilesetInfo* tileset = (CCTMXTilesetInfo*)pTMXMapInfo->getTilesets()->lastObject();
CCSize s;
s.width = (float)atof(valueForKey("x", attributeDict));
s.height = (float)atof(valueForKey("y", attributeDict));
tileset->m_tTileOffset = s;
}
...
}
void CCTMXLayer::setupTileSprite(CCSprite* sprite, CCPoint pos, unsigned int gid)
{
CCPoint np = positionAt(pos);
np.x += m_pTileSet->m_tTileOffset.width;
np.y -= m_pTileSet->m_tTileOffset.height;
sprite->setPosition(np);
...
}
2.PVR support
cocos2d: WARNING: unsupported pvr pixelformat
cocos2dx据说默认支持mac下texturetool生成的pvr图片,但鄙人没有苹果电脑,故使用PVRTexTool来生成pvr图片
在windows测试时,发现cocos2dx并不支持pvrtc格式,仅支持九种,android和ios没有测试
/* supported channel type formats */
#define kPVR3TexturePixelFormat_BGRA_8888 0x0808080861726762ULL
#define kPVR3TexturePixelFormat_RGBA_8888 0x0808080861626772ULL
#define kPVR3TexturePixelFormat_RGBA_4444 0x0404040461626772ULL
#define kPVR3TexturePixelFormat_RGBA_5551 0x0105050561626772ULL
#define kPVR3TexturePixelFormat_RGB_565 0x0005060500626772ULL
#define kPVR3TexturePixelFormat_RGB_888 0x0008080800626772ULL
#define kPVR3TexturePixelFormat_A_8 0x0000000800000061ULL
#define kPVR3TexturePixelFormat_L_8 0x000000080000006cULL
#define kPVR3TexturePixelFormat_LA_88 0x000008080000616cULL
所以使用PVRTexTool转换图片时制定它支持的格式比如RGBA8888,如下
PVRTexToolCL -i grassland.png -f r8g8b8a8
想要变成pvr.ccz,只要用winRar用zlib压缩,后缀名改成pvr.ccz。