need help with my mouse

Started by
0 comments, last by beaner 21 years, 5 months ago
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?
Advertisement
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.

This topic is closed to new replies.

Advertisement