Changing the colour of a button when clicked
Posted: Sat Jul 04, 2020 3:24 pm
I'm trying to make a button change colour for a few seconds after it has been clicked but I'm not sure on how to do it.
Currently this is part of my button class code, I know the click detection works because it also plays a sound when clicked:
In main's update function I'd just call box:click().
Now, I tried using that if-else statement in box:render() to change the colour but I think it doesn't work because it'd change the colour for a frame and then it'd go back to normal. Is that right?
Honestly I'm stuck, could anybody please give me some advice?
Currently this is part of my button class code, I know the click detection works because it also plays a sound when clicked:
Code: Select all
function Box:isHovered(x,y)
if x > self.X and x < self.X + self.width and y > self.Y and y < self.Y + self.height then
return true
end
end
function Box:click()
if love.mouse.isDown("1") then
local x, y = love.mouse.getPosition()
if self:isHovered(x,y) then
self.sound:play()
self.clicked = true
end
end
end
function Box:render()
if clicked then
love.graphics.setColor(lighterYellow['r'], lighterYellow['g'], lighterYellow['b'])
else
love.graphics.setColor(self.colour['r'], self.colour['g'], self.colour['b'])
end
love.graphics.rectangle('fill', self.X, self.Y, self.width, self.height)
end
Now, I tried using that if-else statement in box:render() to change the colour but I think it doesn't work because it'd change the colour for a frame and then it'd go back to normal. Is that right?
Honestly I'm stuck, could anybody please give me some advice?