我一直在努力理解Tiled Map Editor 制作的《骑士斗》等轴测游戏
当我打开一个新项目并加载相同的映射时,这个特定的函数会给我不同的结果。
-(CGPoint) locationFromTilePos:(CGPoint)tilePos;
{
CCTMXLayer *grass = [self.tileMap layerNamed:@"Grass"];
CCSprite *tile = [grass tileAt:tilePos];
float x = -tile.position.x - self.tileMap.tileSize.width + 32;
float y = -tile.position.y - self.tileMap.tileSize.height;
return CGPointMake(x, y);
}
当在骑士战斗中以(0,0)的形式输入tilePos时
- 草瓦位置:(1248、1248)
- 函数返回的最终位置:(-1280,-1280)
当在我的新项目中以(0,0)的形式输入tilePos时
- 草瓷砖位置:(624624)
- 函数返回的最终位置:(-656,-656)
我在使用Tiled的Cocos2d上找不到任何关于等轴测地图的在线资源。我需要在平铺坐标和实际屏幕坐标之间进行转换。有人能帮忙吗。
试试这个://从平铺坐标计算屏幕坐标
- (CGPoint)positionForTileCoord:(CGPoint)pos {
float halfMapWidth = _tileMap.mapSize.width*0.5;
float halfMapHeight = _tileMap.mapSize.height*0.5;
float tileWidth = _tileMap.tileSize.width;
float tileHeight = _tileMap.tileSize.height;
int convertedY = _tileMap.mapSize.height-(pos.y-1);
int x = halfMapWidth*tileWidth + tileWidth*pos.x*0.5-tileWidth*pos.y*0.5;
int y = halfMapHeight*tileHeight +tileHeight*convertedY*0.5 - tileHeight*pos.x*0.5-tileHeight*0.5;
return ccp(x, y);
}
相反,试试这个:
// calculating the tile coordinates from screen location
-(CGPoint) tilePosFromLocation:(CGPoint)location
{
CGPoint pos = location;
float halfMapWidth = _tileMap.mapSize.width*0.5;
float mapHeight = _tileMap.mapSize.height;
float tileWidth = _tileMap.tileSize.width;
float tileHeight = _tileMap.tileSize.height;
CGPoint tilePosDiv = CGPointMake(pos.x/tileWidth, pos.y/tileHeight);
float invereseTileY = mapHeight - tilePosDiv.y;
// Cast int to make sure that result is in whole numbers
float posX = (int)(invereseTileY + tilePosDiv.x - halfMapWidth);
float posY = (int)(invereseTileY - tilePosDiv.x + halfMapWidth);
return CGPointMake(posX, posY);
}