Game Development Reference
In-Depth Information
Figure 11-14 . The coordinate system of an isometric tilemap
This may seem strange at first, but if you lean your head a bit to the right, you may no-
tice that the tile coordinates follow the exact same order as in orthogonal tilemaps, ex-
cept that the whole map is rotated by 45 degrees.
You can now straighten your head again, because I need you to focus on the modified
tilePosFromLocation method, which calculates the touched tile coordinates
from a touch location on the screen. As Listing 11-2 shows, it's a wee bit more com-
plex than the orthogonal counterpart.
Listing 11-2. 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 pointWidth = tileMap.tileSize.width / CC_CONTENT_SCALE_FACTOR();
float pointHeight = tileMap.tileSize.height / CC_CONTENT_SCALE_FACTOR();
CGPoint tilePosDiv = CGPointMake(pos.x / pointWidth, pos.y / pointHeight);
float inverseTileY = mapHeight - tilePosDiv.y;
Search Nedrilad ::

Custom Search