How to detect if something was clicked?

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
cybercircuit
Prole
Posts: 16
Joined: Mon May 20, 2013 7:39 pm

How to detect if something was clicked?

Post by cybercircuit »

How can I detect if something was clicked? I know you can detect if the mouse is down and its within the range of the button but I was wondering if there was an easier way...(cuz figuring out the buttons click range thing can be hard and use alot of debugging for a single button)
User avatar
Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

Re: How to detect if something was clicked?

Post by Kingdaro »

There really isn't any other way other than range checking.
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: How to detect if something was clicked?

Post by Plu »

You could try using a collision system like HardonCollider and attaching a single pixel to the tip of the mouse, then scan if the tip-pixel collides with a button shape. Whether or not it'd be an improvement; I'm not sure. I used this way back when I made games and it worked pretty well but I haven't done it in LÖVE yet.
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: How to detect if something was clicked?

Post by veethree »

Once you figure out the math behind the range check, It's really not that complicated. I've attached a quick example.

Here's the code if you don't feel like downloading it.

Code: Select all

function love.load()
	button = {x = 12, y = 12, width = 64, height = 32, c = {255, 255, 255}}--Button object
end

function love.draw()
	love.graphics.setColor(button.c)--Set the color of the button
	love.graphics.rectangle("fill", button.x, button.y,button.width, button.height) --Drawing the button
end

function love.mousepressed(x,y,k)
	if k == "l" then
		if x > button.x and x < button.x + button.width and y > button.y and y < button.height then -- Checks if the mouse is on the button
			for i=1, #button.c do --Simple numeric loop that sets the buttons color to a random one
				button.c[i] = math.random(0, 255)
			end
		end
	end
end
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests