#### Archived

This topic is now archived and is closed to further replies.

# need help with my mouse

This topic is 5750 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am making a 2d tile editor and things are going pretty smoothly. Right now, I can set up and display a random map of any size I desire in my window. Now it is time to start coding the thing so it can edit actual maps, and I need to write code for detecting which tile the mouse arrow is over, so that I can change the tile when it is clicked. However, I have no clue where to start... my book references aren''t doing me much good. Can anyone help?

##### Share on other sites
Sure. If you have the X and Y coordinates of the mouse in relation to the beginning of the tiles, then you can divide the X and Y coordinates by the width and height of the tiles respectively to get which tile the mouse is in. Use an integer division to inherently strip the decimal, which merely represents how far into any given tile we are. The whole part is what denotes the tile. The first tile on both axis is 0.

Think of it this way. The width of each tile can be seen as a unit measure, namely pixels per tile. The X and Y coordinates can be seen as just pixels. So if we have the pixel unit, and we divide it by the pixel per tile unit, we end up with the tile unit - P / (P/T) = T. This is simply a manipulation of units to help demonstrate the solution.

1. 1
Rutin
26
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 9
• 9
• 9
• 46
• 41
• ### Forum Statistics

• Total Topics
631750
• Total Posts
3002059
×