45度地图 实际坐标与瓷砖坐标的互相转换
http://blog.sina.com.cn/s/blog_6056c88301010z6q.html
CCPoint DWMap::tilePosFromLocation(CCPoint location)
{
// 触摸的位置信息必须减去瓷砖地图的位置信息,因为地图的位置可能在滚动变化
CCPoint pos = ccpSub(location, m_pTileMap->getPosition());
float halfMapWidth = m_pTileMap->getMapSize().width * 0.5f;
float mapHeight = m_pTileMap->getMapSize().height;
float tileWidth = 0.0f;
float tileHeight = 0.0f;
//这里需要注意Retina中的TileSize
if(CCDirector::sharedDirector()->isRetinaDisplay())
{
tileWidth = m_pTileMap->getTileSize().width / 2;
tileHeight = m_pTileMap->getTileSize().height / 2;
}
else
{
tileWidth = m_pTileMap->getTileSize().width;
tileHeight = m_pTileMap->getTileSize().height;
}
CCPoint tilePosDiv = CCPointMake(pos.x / tileWidth, pos.y / tileHeight);
float inverseTileY = mapHeight - tilePosDiv.y;
// 将得到的计算结果转换成 int,以确保得到的是整数
float posX = (int)(inverseTileY + tilePosDiv.x - halfMapWidth);
float posY = (int)(inverseTileY - tilePosDiv.x + halfMapWidth);
return CCPointMake(posX, posY);
}
CCPoint DWMap::LocationFromtilePos(CCPoint tilePos)
{
float halfMapWidth = m_pTileMap->getMapSize().width * 0.5f;
float mapHeight = m_pTileMap->getMapSize().height;
float tileWidth = 0.0f;
float tileHeight = 0.0f;
if(CCDirector::sharedDirector()->isRetinaDisplay())
{
tileWidth = m_pTileMap->getTileSize().width / 2;
tileHeight = m_pTileMap->getTileSize().height / 2;
}
else
{
tileWidth = m_pTileMap->getTileSize().width;
tileHeight = m_pTileMap->getTileSize().height;
}
CCPoint tilePosDiv;
CCPoint pos;
tilePosDiv.y = mapHeight - (tilePos.x + tilePos.y)/2;
tilePosDiv.x = (tilePos.x - tilePos.y)/2 + halfMapWidth;
pos.x = tilePosDiv.x * tileWidth;
pos.y = tilePosDiv.y * tileHeight;
return ccpAdd(m_pTileMap->getPosition(),pos);
}

浙公网安备 33010602011771号