As the title says, is there any nice and easy (most importantly, working) way to create a button?
I'm here from an engine that used listeners and I see that in LÖVE we put everything in love.update(dt) (at least "listeners", according to my knowledge). The question then is, how can I detect if player actually presses the button. I can surely make a circle on press and just see if it collides, but I really hope there's a LOT easier way that I just don't see yet. I hope I'll get help
I'm searching for an easy way to create button
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 26
- Joined: Thu Feb 14, 2019 7:45 pm
Re: I'm searching for an easy way to create button
If you just want a button and no other UI, then the simplest solution is to check if the mouse was pressed in the borders of the button. Something like:
Code: Select all
local buttonBounds = {x = 10, y = 10, width = 200, height = 80}
function love.draw()
love.graphics.setColor(1, 0, 0)
love.graphics.rectangle("fill", buttonBounds.x, buttonBounds.y, buttonBounds.width, buttonBounds.height)
end
function love.mousepressed(x, y, button, istouch, presses)
if x > buttonBounds.x and x < buttonBounds.x + buttonBounds.width and y > buttonBounds.y and y < buttonBounds.y + buttonBounds.height then
print "Button pressed!"
end
end
LÖVE-Nuklear - a lightweight immediate mode GUI for LÖVE games
-
- Prole
- Posts: 26
- Joined: Thu Feb 14, 2019 7:45 pm
Re: I'm searching for an easy way to create button
That's exactly what I was saying, I just thought there's a more simple and better way of implementing a button already built in. Thanks for answering anyway!keharriso wrote: ↑Fri Feb 15, 2019 12:26 am If you just want a button and no other UI, then the simplest solution is to check if the mouse was pressed in the borders of the button. Something like:
Code: Select all
local buttonBounds = {x = 10, y = 10, width = 200, height = 80} function love.draw() love.graphics.setColor(1, 0, 0) love.graphics.rectangle("fill", buttonBounds.x, buttonBounds.y, buttonBounds.width, buttonBounds.height) end function love.mousepressed(x, y, button, istouch, presses) if x > buttonBounds.x and x < buttonBounds.x + buttonBounds.width and y > buttonBounds.y and y < buttonBounds.y + buttonBounds.height then print "Button pressed!" end end
Who is online
Users browsing this forum: Bing [Bot] and 81 guests