1

我正在尝试将等距平铺坐标转换为屏幕坐标。我似乎有问题,尤其是 Y 坐标,看起来 X 部分工作得很好。这是我到目前为止得到的。

// calculate screen coordinates from tile coordinates

- (CGPoint)positionForTileCoord:(CGPoint)pos {

float halfMapWidth = _tileMap.mapSize.width*0.5;
float mapHeight = _tileMap.mapSize.height;
float tileWidth = _tileMap.tileSize.width;
float tileHeight = _tileMap.tileSize.height;


int x = halfMapWidth*tileWidth + tileWidth*pos.x*0.5-tileWidth*pos.y*0.5;

int y =  ............


return ccp(x, y);

我的播放器作为子元素添加到 Tile 地图本身,地图添加到 screenSize.x/2、scrrensize.y/2 的图层,锚点为 0.5

我用正交地图成功地完成了同样的事情,但似乎在使用等距地图时遇到了困难。

谢谢

4

2 回答 2

4

真的是这样的:

  // calculate screen coordinates from tile coordinates
    - (CGPoint)positionForTileCoord:(CGPoint)pos {

        float halfMapWidth = _tileMap.mapSize.width*0.5;
        float mapHeight = _tileMap.mapSize.height;
        float tileWidth = _tileMap.tileSize.width;
        float tileHeight = _tileMap.tileSize.height;


        int x = halfMapWidth*tileWidth + tileWidth*pos.x*0.5-tileWidth*pos.y*0.5;

        int y = (pos.y + (mapHeight * tileWidth/2) - (tileHeight/2)) - ((pos.y + pos.x) *   tileHeight/2) + tileHeight;   

        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);
    }
于 2013-10-17T21:56:31.497 回答
1
int y = (pos.y + (mapHeight * tileWidth/2) - (tileHeight/2)) - ((pos.y + pos.x) *   tileHeight/2) + tileHeight;
于 2013-08-07T19:24:55.197 回答