How to detect if something was clicked?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 16
- Joined: Mon May 20, 2013 7:39 pm
How to detect if something was clicked?
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)
Re: How to detect if something was clicked?
There really isn't any other way other than range checking.
Re: How to detect if something was clicked?
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.
Re: How to detect if something was clicked?
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.
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
Who is online
Users browsing this forum: No registered users and 2 guests