[XNA] Selecting tiles in a match3 game

Started by
2 comments, last by GWDev 12 years, 3 months ago
Hi all,
I am slowly assembling my first XNA game, a match3 one (maybe I should have started with Tetris... biggrin.png).
Basically, you click with the mouse on a tile and drag the pointer around to select other adjacent tiles, pretty simple. The problem is I'd also like to select tiles in diagonal and this doesn't work.
Right now, I take the mouse position, check if it falls within my board boundaries and then divide the X and Y coordinates by the tiles' width and height to get the currently selected tile. So, for example, when I move the pointer in up-right direction it either selects the tile above or the tile on the left.
I tried leaving a margin of sorts, say 8 pixels, but in this way I am able to move the mouse between tiles and select non adjacent ones.
I thought I can check a "rounded rectangle" area, rather than a rectangular one, but I have no clue on how to do this..
Any ideas?
Thanks (and Happy New Year!!!)
Advertisement
The easiest thing to do would be to swap the tiles when the mouse button is released. You can simply check the direction of the mouse pointer compared to the center of the original tile and choose a tile to swap. For example, if the mouse was released at (40, 40) and the center of the clicked tile was at (0,0), the angle will be 45 degrees. If the angle is between 30 degrees and 60 degrees (assuming 0 is east and 180 is west), you could assume the player meant northeast. After that it is a matter of tweaking the numbers and angles to make it more accurate: maybe you want a smaller angle to allow for diagonal dragging, or maybe you also want to take into account the original mouse click position rather than the center of the clicked tile.

If you want the tiles to swap while dragging, you usually want this to happen when your mouse exits the current tile. So you could make a bit of code that checks which tile was clicked first, and then keep checking the coordinates until the mouse leaves the original tile. Then you get the coordinates of the place where the tile was left, and you can calculate the direction of movement compared to the center of the tile.

Good luck, and happy new year to you too!
Guido
Many thanks for your reply!
I didn't think I could handle this by checking the angle, I will give it a try.
Cheers!
Hello harlock1975,

you could store the information about the tiles in a gridlike datastructure. Say at the beginning you have 10 x 10 tiles on the board. tile 1 (or 0) is the upper left. You know how big your tile is, so you know the coordinates on the screens to check on mouseclicks. And you can easily check if an allowed tile was clicked.

Example:
First click ist on 545 / 765 ... you find in your data that this means it is tile 45.
Now you know that the allowed tiles for the second click are the ones next to it. Means above (45-10 = 35), under (45+10 = 55), left (45 -1 = 44), right (45 + 1 = 46), upper-left-corner (45-11 = 34), upper-right-corner (45 - 9 = 36) and so on.
As you know how big your tiles are (and the space between them) you know exactly the positions (max, min) of all of them.
Just check for the special cases on the border. first row / column and last row / column.

An Dictionary of Tile-objects a multidimensional array would work or whatever you are comfortable with.

--GWDev

This topic is closed to new replies.

Advertisement