3
\$\begingroup\$

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?

\$\endgroup\$
  • \$\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 May 16 '14 at 15:32
  • \$\begingroup\$ I never heard of nearestPiece(tilemap, playerTouchPos) so I will have to research it more. \$\endgroup\$ – zardon May 16 '14 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 May 16 '14 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 May 16 '14 at 16:10
  • \$\begingroup\$ Okay, this makes sense. \$\endgroup\$ – zardon May 19 '14 at 8:43
2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Thanks. I think I was getting confused. Upvoted and accepted. Not enough cred to upvote at present. \$\endgroup\$ – zardon May 19 '14 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 May 19 '14 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 May 19 '14 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 May 20 '14 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.