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?

  • \$\begingroup\$ 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?) \$\endgroup\$
    – Anko
    Commented May 16, 2014 at 15:32
  • \$\begingroup\$ I never heard of nearestPiece(tilemap, playerTouchPos) so I will have to research it more. \$\endgroup\$
    – zardon
    Commented May 16, 2014 at 16:06
  • \$\begingroup\$ 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 \$\endgroup\$
    – zardon
    Commented May 16, 2014 at 16:07
  • \$\begingroup\$ All of that was pseudocode; only structurally illustrative. Don't bother looking the functions up: They don't exist (yet)! :) \$\endgroup\$
    – Anko
    Commented May 16, 2014 at 16:10
  • \$\begingroup\$ Okay, this makes sense. \$\endgroup\$
    – zardon
    Commented May 19, 2014 at 8:43

1 Answer 1


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

Tile coordinates count tiles.

tile coordinates

Pixel coordinates count pixels.

pixel coordinates

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:

How the solution works

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.

  • \$\begingroup\$ Thanks. I think I was getting confused. Upvoted and accepted. Not enough cred to upvote at present. \$\endgroup\$
    – zardon
    Commented May 19, 2014 at 16:23
  • \$\begingroup\$ 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. \$\endgroup\$
    – zardon
    Commented May 19, 2014 at 16:25
  • \$\begingroup\$ @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.) \$\endgroup\$
    – Anko
    Commented May 19, 2014 at 16:44
  • \$\begingroup\$ 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. \$\endgroup\$
    – zardon
    Commented May 20, 2014 at 9:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .