CCLayerPanZoom扩展和等距tilemap

时间:2013-09-21 11:07:22

标签: cocos2d-x cclayerpanzoom

我在使用CCLayerPanZoom扩展程序时从触摸位置获取切片坐标时遇到问题。它没有缩放效果很好(当缩放为1.0f时),但是如果我放大或缩小它会返回奇怪的坐标。这是我的tilePosFromLocation方法:

CCPoint pos = ccpSub(location, _panZoomLayer->getPosition());
float halfMapWidth = tileMap->getMapSize().width * 0.5f;    
float mapHeight = tileMap->getMapSize().height;     
float tileWidth = tileMap->getTileSize().width;     
float tileHeight = tileMap->getTileSize().height;   
CCPoint tilePosDiv = CCPointMake(pos.x / tileWidth, pos.y / tileHeight); 
float inverseTileY = mapHeight - tilePosDiv.y;
float posX = (int)(inverseTileY + tilePosDiv.x - halfMapWidth); 
float posY = (int)(inverseTileY - tilePosDiv.x + halfMapWidth);
posX = MAX(0, posX); 
posX = MIN(tileMap->getMapSize().width - 1, posX);  
posY = MAX(0, posY); 
posY = MIN(tileMap->getMapSize().height- 1, posY); 
pos=ccp(posX, posY);
return pos;

如果我使用directyl tileMap->setScale()代替扩展名,那么如果我将切片维度与当前地图比例数量相乘,则上述代码可以正常工作。我会使用它而不是CCLayerPanZoom但是如果没有这个扩展名我就无法进行平滑和漂亮的缩放。

有关如何使其有效的建议吗?

1 个答案:

答案 0 :(得分:1)

做这个......

location=_panZoomLayer->convertToNodeSpace(location); //location is touchLocation ..after convertToGL


convertToNodeSpace method will handle any changes like position or scaling to layer