1

我正在编写一些代码以将等距 CCTMXTiledMap 放置到 CCLayerPanZoom 控件上,然后将触摸位置转换为 ISO tilemap 坐标。这一切对我来说都非常好,只要 CClayerPanZoom 的比例为 1(即,如果我不放大或缩小)。我可以平移地图并仍然计算正确的 iso tile co-oridinates。但是,一旦我放大或缩小平铺地图,我的代码返回的等坐标是完全错误的。请参阅下面的代码来计算触摸位置的等坐标。

-(CGPoint) tilePosFromLocation:(CGPoint)location tileMap:(CCTMXTiledMap*)thisTileMap panZoom:(CCLayerPanZoom*)thisPanZoom
{   
float midScale = (thisPanZoom.minScale + thisPanZoom.maxScale) / 2.0;

float newScale = (thisPanZoom.scale <= midScale) ? thisPanZoom.maxScale : thisPanZoom.minScale;
if (thisPanZoom.scale < 1)
{        
    newScale = newScale + thisPanZoom.scale;
}
else
{
    newScale = newScale - thisPanZoom.scale;
}

CGFloat deltaX = (location.x  - thisPanZoom.anchorPoint.x * (thisPanZoom.contentSize.width / CC_CONTENT_SCALE_FACTOR()) ) * (newScale);
CGFloat deltaY = (location.y  - thisPanZoom.anchorPoint.y * (thisPanZoom.contentSize.height / CC_CONTENT_SCALE_FACTOR()) ) * (newScale);

CGPoint position = ccp((thisPanZoom.position.x - deltaX) , (thisPanZoom.position.y - deltaY) );

float halfMapWidth = thisTileMap.mapSize.width * 0.5f;
float mapHeight = thisTileMap.mapSize.height;
float tileWidth = thisTileMap.tileSize.width / CC_CONTENT_SCALE_FACTOR() * newScale;
float tileHeight = thisTileMap.tileSize.height / CC_CONTENT_SCALE_FACTOR() * newScale;

CGPoint tilePosDiv = CGPointMake(position.x / tileWidth, position.y / tileHeight );
float inverseTileY = tilePosDiv.y - (mapHeight * CC_CONTENT_SCALE_FACTOR()) * newScale; //mapHeight + tilePosDiv.y;

float posX = (int)(tilePosDiv.y - tilePosDiv.x + halfMapWidth);
float posY = (int)(inverseTileY + tilePosDiv.x - halfMapWidth + mapHeight);


// make sure coordinates are within isomap bounds
posX = MAX(0, posX);
posX = MIN(thisTileMap.mapSize.width - 1, posX);
posY = MAX(0, posY);
posY = MIN(thisTileMap.mapSize.height - 1, posY);

return CGPointMake(posX, posY);
}

任何人都可以提供任何关于我在哪里出错的见解吗?

谢谢,艾伦

4

0 回答 0