使用cocos2d的等距平铺游戏中的问题

时间:2013-08-11 19:03:58

标签: iphone ios cocos2d-iphone isometric

我一直在努力理解用Knight Fight制作的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);
}

在骑士战斗中将tilePos作为(0,0)进行喂食

  1. Grass Tile职位:(1248,1248)
  2. 函数返回的最终位置:( - 1280,-1280)
  3. 在我的新项目中将tilePos作为(0,0)提供

    1. Grass Tile职位:(624,624)
    2. 函数返回的最终位置:( - 656,-656)
    3. 我无法使用Tiled在Cocos2d上找到Isometric Maps的任何资源。我需要在平铺坐标和真实屏幕坐标之间进行转换。任何人都可以提供帮助。

1 个答案:

答案 0 :(得分:2)

试试这个:    //从图块坐标计算屏幕坐标

- (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);
}