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
Checking whether text is under mouse
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Checking whether text is under mouse
- Attachments
-
- Pong.love
- (5.61 KiB) Downloaded 328 times
Re: Checking whether text is under mouse
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
Re: Checking whether text is under mouse
Could you please explain to me this bit:
Code: Select all
for name,t in pairs(menu) do
- mastastealth
- Prole
- Posts: 30
- Joined: Thu Jul 03, 2008 2:44 am
- Location: Barranquilla, Colombia
- Contact:
Re: Checking whether text is under mouse
As far as I understand, what it's doing is going down each item in the "menu" array which is:
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.
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 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.
Re: Checking whether text is under mouse
Code: Select all
for name,t in pairs(menu) do
So in the code above, the next key/value pair will be stored in name/t for each loop iteration.
Re: Checking whether text is under mouse
I see! Thanks.
Who is online
Users browsing this forum: Ahrefs [Bot], glitchapp, Google [Bot] and 85 guests