在CCLayerPanZoom控件上放大时计算TMX贴图的iso tile坐标

时间:2013-08-02 18:28:19

标签: cocos2d-iphone cclayerpanzoom

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

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

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

谢谢, 艾伦

0 个答案:

没有答案