Checking whether text is under mouse

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
MrPickle
Prole
Posts: 24
Joined: Thu Jul 10, 2008 11:43 pm
Location: United Kingdom, Lincolnshire

Checking whether text is under mouse

Post by MrPickle »

I am trying to make pong so I can teach myself a few things about Lua, I'm trying to do the menu at the moment.
I want the text to change colour when the mouse goes over it, I _sort_ of have it working but it doesn't work very well.

I have attached a .love file

P.S. I'm sorry for the amount of topics I'm making, I do try to solve these myself but you know :(
Attachments
Pong.love
(5.61 KiB) Downloaded 328 times
User avatar
rude
Administrator
Posts: 1052
Joined: Mon Feb 04, 2008 3:58 pm
Location: Oslo, Norway

Re: Checking whether text is under mouse

Post by rude »

The easiest thing to do is to use hitboxes. I hacked together a quick example for you.
Attachments
menu.love
(829 Bytes) Downloaded 348 times
MrPickle
Prole
Posts: 24
Joined: Thu Jul 10, 2008 11:43 pm
Location: United Kingdom, Lincolnshire

Re: Checking whether text is under mouse

Post by MrPickle »

Could you please explain to me this bit:

Code: Select all

for name,t in pairs(menu) do
User avatar
mastastealth
Prole
Posts: 30
Joined: Thu Jul 03, 2008 2:44 am
Location: Barranquilla, Colombia
Contact:

Re: Checking whether text is under mouse

Post by mastastealth »

As far as I understand, what it's doing is going down each item in the "menu" array which is:

Code: Select all

    ["Start"] = { x = 300, y = 225, w = 200, h = 25 },
    ["Options"] = { x = 300, y = 255, w = 200, h = 25 },
    ["Destroy Humans"] = { x = 300, y = 285, w = 200, h = 25 },
    ["Awaken the Dead"] = { x = 300, y = 315, w = 200, h = 25 },
    ["Quit"] = { x = 300, y = 345, w = 200, h = 25 },
So you have those 5 items, it looks like the name is being used as the key, with the values being various variables to create your hitboxes.

So the code in the draw() is going down each menu item ("name", the key) , checking if the mouse position (x,y) is inside the hitbox (which is the variable "t", which is the value being passed into the for loop) and changing the color of the text accordingly. :)
User avatar
rude
Administrator
Posts: 1052
Joined: Mon Feb 04, 2008 3:58 pm
Location: Oslo, Norway

Re: Checking whether text is under mouse

Post by rude »

Code: Select all

for name,t in pairs(menu) do
There are two types of for loops in Lua, numeric for, which you already know, and generic for. Generic for operates on iterators, and pairs() returns an iterator for the table passed as the parameter.

So in the code above, the next key/value pair will be stored in name/t for each loop iteration.
MrPickle
Prole
Posts: 24
Joined: Thu Jul 10, 2008 11:43 pm
Location: United Kingdom, Lincolnshire

Re: Checking whether text is under mouse

Post by MrPickle »

I see! Thanks.
Post Reply

Who is online

Users browsing this forum: DOMINOSRULZ and 89 guests