Page 1 of 1
How to play a sound when I hover over a button?
Posted: Sun Jan 08, 2017 7:05 am
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?
Re: How to play a sound when I hover over a button?
Posted: Sun Jan 08, 2017 10:26 am
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.
Re: How to play a sound when I hover over a button?
Posted: Sun Jan 08, 2017 12:43 pm
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.
Re: How to play a sound when I hover over a button?
Posted: Sun Jan 08, 2017 1:56 pm
by xNick1
I don't know if it can help, but
works for me.
This one:
Code: Select all
sound = love.audio.newSource("assets/menusfs.mp3", "static")
has to be put in love.load()
Re: How to play a sound when I hover over a button?
Posted: Sun Jan 08, 2017 3:58 pm
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
Re: How to play a sound when I hover over a button?
Posted: Sun Jan 08, 2017 4:20 pm
by JacobTheFox
Yay, it's working now. Thank you!