# How do I convert the coordinates of a click to a tilemap position?

How do you turn a tilemap (produced by TileD) for cocos2d where I can click on individual items and get a callback?

Is it just a case of converting captured touch and finding out where it is on the tilemap and then converting that to pixels -- but that doesn't say what item was clicked on.

I have a tilemap of a city with buildings on it, and I'd like to be able to click or press on a specific building, get a callback.

What would be the best way to accomplish this?

• I imagine you know where the player touched, and you know the location and contents of each tile. Why not just use that to deduce where the player touched? return nearestPiece(tilemap, playerTouchPos).contents or similar? (Or is how to do that what you're asking?)
– Anko
May 16 '14 at 15:32
• I never heard of nearestPiece(tilemap, playerTouchPos) so I will have to research it more. May 16 '14 at 16:06
• Yes, I am asking how to convert the contents of where a person has touched into finding out what I clicked on. So if its building XYZ or whatever May 16 '14 at 16:07
• All of that was pseudocode; only structurally illustrative. Don't bother looking the functions up: They don't exist (yet)! :)
– Anko
May 16 '14 at 16:10
• Okay, this makes sense. May 19 '14 at 8:43

I think you're getting your pixel coordinates and tilemap coordinates confused.

Tile coordinates count tiles.

Pixel coordinates count pixels.

To convert from a pixel coordinate to a tile coordinate, divide the click_x by the tile width and the click_y by the and tile height, to get the tile coordinates x and y. (Discard the remainders to get an integer.)

The reason this works is pretty clear if we overlay the two coordinate systems:

In this above case, the pixel coordinates of the click would correspond to 6,4 in tile coordinates.

Once you know what tile position a click corresponds to, it should be trivial to find the contents of that tile.

• Thanks. I think I was getting confused. Upvoted and accepted. Not enough cred to upvote at present. May 19 '14 at 16:23
• I really need to get myself a basic tilemap built using dummy graphics and start coding up a touch interface and see what I can do with it -- I have no idea how I'm going to be able to keep a check of the multiple different kinds of buildings on the screen, but I'm sure with the notes you've given me I should get a bit further ahead. May 19 '14 at 16:25
• @zardon I'm happy if this helped. In most languages, I see game programmers store tilemaps as two-dimensional arrays of "tile" objects (buildings, grass; whatever your tiles contain). That array's indices are then effectively tile coordinates, so myTilemap[6][4] would store whatever building stands at tile coordinates 6,4. (I've never used cocos2d though; a more specific tutorial is probably easiest to follow.)
– Anko
May 19 '14 at 16:44
• Even just theory is good enough, I can work out the cocos2d stuff; I agree array indicies will help. I guess its case of having tilemaps and it tells the computer what is there, and the array indicies tell the computer what they do & what to call (function wise) and the pressing on the map tells the computer where the press occured. May 20 '14 at 9:18