Returns tile coordinates according to the coordinates of the touch point locating an isometric tile

Source: Internet
Author: User

"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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.