How to play a sound when I hover over a button?

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
JacobTheFox
Prole
Posts: 4
Joined: Sun Jan 08, 2017 7:01 am

How to play a sound when I hover over a button?

Post by JacobTheFox »

Hello people of the Love2D Community, I'm having a few troubles getting a sound to only play once while hovering over a button. Heres my code:

Code: Select all

sound = love.audio.newSource("assets/menusfs.mp3", "static")

Code: Select all

function button_draw()
   for i,v in ipairs(button) do
   
	  if v.mouseover == false then
	  love.graphics.setColor(0,0,0)
			end
	  if v.mouseover == true then
	  love.graphics.setColor(0,255,255)
	  end
	  love.graphics.setFont(medium)
	  love.graphics.print(v.text,v.x,v.y)
		
   end		
end
I think those are the parts I'd need to edit to make it work. Any ideas?
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How to play a sound when I hover over a button?

Post by zorg »

you don't show where you set the buttons' mouseover field though... but in there, just do a love.audio.play(sound)... or a sound:play().
Unless you set it up as looping, it should only play once.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
JacobTheFox
Prole
Posts: 4
Joined: Sun Jan 08, 2017 7:01 am

Re: How to play a sound when I hover over a button?

Post by JacobTheFox »

Code: Select all

function button_check()
	for i,v in ipairs(button) do
	if mousex > v.x and
	mousex < v.x + medium:getWidth(v.text) and
	mousey > v.y and
	mousey < v.y + medium:getHeight() then
	 v.mouseover = true
	 else
		v.mouseover = false
		end
	 end
end
Thats the mouseover field. I tried to just do sound:play() but while I'm hovering over the button it just loops the sound until I take my mouse off of it.
User avatar
xNick1
Party member
Posts: 267
Joined: Wed Jun 15, 2016 8:27 am
Location: Rome, Italy

Re: How to play a sound when I hover over a button?

Post by xNick1 »

I don't know if it can help, but

Code: Select all

love.audio.play( sound )
works for me.

This one:

Code: Select all

sound = love.audio.newSource("assets/menusfs.mp3", "static")
has to be put in love.load()
User avatar
Ulydev
Party member
Posts: 445
Joined: Mon Nov 10, 2014 10:46 pm
Location: Paris
Contact:

Re: How to play a sound when I hover over a button?

Post by Ulydev »

JacobTheFox wrote:

Code: Select all

function button_check()
	for i,v in ipairs(button) do
	if mousex > v.x and
	mousex < v.x + medium:getWidth(v.text) and
	mousey > v.y and
	mousey < v.y + medium:getHeight() then
	 v.mouseover = true
	 else
		v.mouseover = false
		end
	 end
end
Thats the mouseover field. I tried to just do sound:play() but while I'm hovering over the button it just loops the sound until I take my mouse off of it.
Try adding this right before v.mouseover = true:

Code: Select all

if not v.mouseover then love.audio.play(sound) end
JacobTheFox
Prole
Posts: 4
Joined: Sun Jan 08, 2017 7:01 am

Re: How to play a sound when I hover over a button?

Post by JacobTheFox »

Yay, it's working now. Thank you!
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 6 guests