码迷,mamicode.com
首页 > 其他好文 > 详细

根据触摸点的坐标返回瓦片坐标 Locating an Isometric Tile

时间:2015-11-25 00:22:45      阅读:304      评论:0      收藏:0      [点我收藏+]

标签:

 

  《Learn iPhone and iPad Cocos2D Game Development》

1. 根据触摸点的坐标返回瓦片坐标。

//根据触摸点的位置计算瓦片坐标


-(CGPoint) tilePosFromLocation:(CGPoint)location tileMap:(CCTMXTiledMap*)tileMap
{
// Tilemap position must be subtracted, in case the tilemap position is scrolling
// 考虑瓦片地图滚动的情况,要减去当前位置
CGPoint pos = ccpSub(location, tileMap.position);
float halfMapWidth = tileMap.mapSize.width * 0.5f;  //地图宽*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 inverseTileY = mapHeight - tilePosDiv.y;
// Cast to int makes sure that result is in whole numbers
float posX = (int)(inverseTileY + tilePosDiv.x - halfMapWidth);
float posY = (int)(inverseTileY - tilePosDiv.x + halfMapWidth);
// make sure coordinates are within isomap bounds
posX = MAX(0, posX);
posX = MIN(tileMap.mapSize.width - 1, posX);
posY = MAX(0, posY);
posY = MIN(tileMap.mapSize.height - 1, posY);
return CGPointMake(posX, posY);
}

  

 

 

Calculating the Tile Coordinates from a Touch Location
-(CGPoint) tilePosFromLocation:(CGPoint)location tileMap:(CCTMXTiledMap*)tileMap
{
// Tilemap position must be subtracted, in case the tilemap position is
scrolling
CGPoint pos = ccpSub(location, tileMap.position);
float halfMapWidth = tileMap.mapSize.width * 0.5f;
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 inverseTileY = mapHeight - tilePosDiv.y;
// Cast to int makes sure that result is in whole numbers
float posX = (int)(inverseTileY + tilePosDiv.x - halfMapWidth);
float posY = (int)(inverseTileY - tilePosDiv.x + halfMapWidth);
// make sure coordinates are within isomap bounds
posX = MAX(0, posX);
posX = MIN(tileMap.mapSize.width - 1, posX);
posY = MAX(0, posY);
posY = MIN(tileMap.mapSize.height - 1, posY);
return CGPointMake(posX, posY);
}

根据触摸点的坐标返回瓦片坐标 Locating an Isometric Tile

标签:

原文地址:http://www.cnblogs.com/xiaosafeimao/p/4993377.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!