Page 1 of 1

Buttons using love.mousepressed?

Posted: Fri Apr 17, 2015 5:57 am
by RBXcpmoderator12345
If I use an if statement to check the x and y of the mouse, and if it was clicked, it will only run if the mouse was on that certain position.
How would I make it so that when a sprite's area is clicked, it would run a block of code?
Um.. this is what I mean...

Code: Select all

function love.mousepressed(x, y, button)
if x == xcoord and y == ycoord and button == "l" then -- xcoord and ycoord are the coordinates of the button, but it would only trigger on that ---exact position
--code
end

Re: Buttons using love.mousepressed?

Posted: Fri Apr 17, 2015 8:09 am
by Robin
Suppose the width and height are stored in the variables button_width and button_height respectively, you'd use:

Code: Select all

function love.mousepressed(x, y, button)
if x >= xcoord and y >= ycoord and x <= xcoord + button_width and y <= ycoord + button_height and button == "l" then
--code
end

Re: Buttons using love.mousepressed?

Posted: Sat Apr 18, 2015 1:42 am
by RBXcpmoderator12345
Ok, attaching a .love
https://www.mediafire.com/?2gcjliirjw8evzd

and btw i tried making the default program to open love files windows explorer because i wanted to see the files without changing it to a zip, so i did that, and now i cant open .loves without dragging them to love.exe.... and i can't set love.exe to the default program to open .love files...

Re: Buttons using love.mousepressed?

Posted: Sat Apr 18, 2015 8:29 am
by Robin
That code is a mess. What is your question? I don't see any code for buttons.

Re: Buttons using love.mousepressed?

Posted: Sat Apr 18, 2015 5:44 pm
by RBXcpmoderator12345
nevermind, i didn't realize i added it yet (the button code)