Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Mouse over Tiles

Author
Message
fllwthewolves
13
Years of Service
User Offline
Joined: 4th Sep 2011
Location:
Posted: 1st Jan 2012 21:58
so im trying to write a game which requires me to create a series of tiles over the screen and then figure out which tile the mouse is over. to create the tiles i used this



i used a array defined as a type to measure out the coordinates of the grid. my problem is that i cant figure out a way to figure out which tile the mouse is over... any ideas?
Chris Tate
DBPro Master
16
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 1st Jan 2012 22:18 Edited at: 1st Jan 2012 22:32
Hi

There are two commands which reveal the location of the mouse cursor within the DBPRO window.

MouseX() & MouseY()

So you can check which grid tile overlaps the mouse using an expression:



There are other ways to solve the problem but this is the easiest way I can think of.



You could also calculate the tile mathematically using the following:
x = Floor( MouseX() / woftile ) + 1
y = Floor( MouseY() / hoftile ) + 1
TileUnderMouse = x + ( ( y - 1 ) * 30 )

The calculation process will run faster but is more difficult to read. I've assumed your array index starts at 1. If not, deduct 1 from TileUnderMouse in the last line of the calculation example.

Edit: Note that if the mouse position is out of tile range, the wrong tile will be returned; so in both examples, you should check that the mouse is within the grid; if it is not then set the TileUnderMouse as zero, to indicate that there is no tile under the mouse.

fllwthewolves
13
Years of Service
User Offline
Joined: 4th Sep 2011
Location:
Posted: 1st Jan 2012 22:29
if i put that inside my main loop wont it kill my speed?

either way thanks so far its working great.
Chris Tate
DBPro Master
16
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 1st Jan 2012 22:39
Quote: "x = Floor( MouseX() / woftile ) + 1
y = Floor( MouseY() / hoftile ) + 1
TileUnderMouse = x + ( ( y - 1 ) * 30 )"


Using the calculation method will work faster than the for loop; but if you have to update each tile anyway, there is no difference; you are already iterating through them so you may as well use any existing for loop for this and similar expressions.

Quote: "Edit: Note that if the mouse position is out of tile range, the wrong tile will be returned; so in both examples, you should check that the mouse is within the grid; if it is not then set the TileUnderMouse as zero, to indicate that there is no tile under the mouse."


Most of todays computers will update 100s of tiles pretty swiftly anyway; it falls down to how much is going on during your game.

LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 2nd Jan 2012 01:52
While I love using TYPE variables, this is one case where I would use a two-dimensional array (i.e. dim grid(30,30) ) instead.



This code assumes that you set the TileWidth and TileHeight variables. I would not cycle through all 900 possibilities, as that would slow it down a lot.

So many games to code.......so little time.

Agent
20
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 2nd Jan 2012 06:33
I use the mathematical solution, myself. You won't find that calculations such as this slow your cycle speed by a noticeable amount unless you are misusing it by putting it inside redundant loops.

I suggest you don't place this code inside your mainloop - make a function of it that returns the tile in question. That way you only need to call the code once at a time, such as when you click. At the very least, only call code like this when you intend to use the result immediately. There's no need to calculate the tile position every mainloop cycle unless you are going to use it every single cycle.

Login to post a reply

Server time is: 2024-11-22 06:31:25
Your offset time is: 2024-11-22 06:31:25