I'm searching for an easy way to create 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
MightyPancake
Prole
Posts: 26
Joined: Thu Feb 14, 2019 7:45 pm

I'm searching for an easy way to create button

Post by MightyPancake »

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 ;)
User avatar
keharriso
Citizen
Posts: 98
Joined: Fri Nov 16, 2012 9:34 pm

Re: I'm searching for an easy way to create button

Post by keharriso »

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
MightyPancake
Prole
Posts: 26
Joined: Thu Feb 14, 2019 7:45 pm

Re: I'm searching for an easy way to create button

Post by MightyPancake »

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
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!
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 81 guests