"Learn IPhone and IPad cocos2d Game development"
1. Return tile coordinates according to the coordinates of the touch point.
Calculate tile coordinates based on the location of the touch points-(Cgpoint) Tileposfromlocation: (cgpoint) site Tilemap: (cctmxtiledmap*) tilemap{//Tilemap Position must be subtracted, in case the Tilemap position is scrolling//consider the situation of tile map scrolling, to subtract the current position cgpoint pos = ccpsub (location, tilemap.position); Float halfmapwidth = TileMap.mapSize.width * 0.5f; Map width *0.5float mapheight = tileMap.mapSize.height; Map High float tilewidth = tileMap.tileSize.width; Tile wide float tileheight = tileMap.tileSize.height; Tile high Cgpoint tileposdiv = Cgpointmake (Pos.x/tilewidth, pos.y/tileheight); float Inversetiley = Mapheight-tileposdiv.y ;//Cast to int makes sure this result is in whole numbersfloat PosX = (int) (Inversetiley + tileposdiv.x-halfmapwidth); f Loat PosY = (int) (inversetiley-tileposdiv.x + halfmapwidth);/Make sure coordinates is within isomap BOUNDSPOSX = MAX ( 0, PosX);p osx = min (tilemap.mapsize.width-1, posX);p osy = MAX (0, PosY);p osy = min (tilemap.mapsize.height-1, PosY); Retu RN 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 was in whole numbers
float PosX = (int) (Inversetiley + tileposdiv.x-halfmapwidth);
float PosY = (int) (inversetiley-tileposdiv.x + halfmapwidth);
Make sure coordinates is 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);
}
Returns tile coordinates according to the coordinates of the touch point locating an isometric tile