Page 1 of 2

love.mousepressed how does it work?

Posted: Sun Sep 22, 2013 2:26 am
by Fatmat927
hi again im asking so many questions hahaha anyways i just dont understand how love.mousepressed works because i want to make a play button on my menu but i dont know how can someone help me? i know i should use the wiki but i dont understand it at all so yeah :/

Re: love.mousepressed how does it work?

Posted: Sun Sep 22, 2013 3:25 am
by adnzzzzZ
When you click with the mouse that function is called. If you have love.mousepressed(x, y, button) defined somewhere then whatever you put in there will be run when the mouse clicked, and the variables x, y will have the position of the click, while the variable button will have which button of the mouse was clicked.

So, the example on the wiki (http://love2d.org/wiki/love.mousepressed) is saying that if button == "l" (which means the left mouse button, you can see all constants here: http://love2d.org/wiki/MouseConstant), then the variables printx and printy will have the position x and y respectively, which was the position where the mouse click happened. Since love.draw is drawing "Text" at the position printx, printy, that program will print "Text" wherever you left-click.

Re: love.mousepressed how does it work?

Posted: Sun Sep 22, 2013 4:01 pm
by Fatmat927
okay but for the (x, y, button) for the x and y can i tell him like a range of where the click must be or is it somewhere else and if i can, how?

Re: love.mousepressed how does it work?

Posted: Sun Sep 22, 2013 5:09 pm
by Robin
You can check it like this: (just example code)

Code: Select all

function love.mousepressed(x, y, button)
    if x >= button_x and x <= button_x + button_width and y >= button_y and y <= button_y + button_height then
        -- the button was clicked!
    end
end
If you have more than one button, you'd usually want to use a table to keep track of the buttons.

Re: love.mousepressed how does it work?

Posted: Sun Sep 22, 2013 8:21 pm
by jjmafiae
Robin wrote:You can check it like this: (just example code)

Code: Select all

function love.mousepressed(x, y, button)
    if x >= button_x and x <= button_x + button_width and y >= button_y and y <= button_y + button_height then
        -- the button was clicked!
    end
end
If you have more than one button, you'd usually want to use a table to keep track of the buttons.
even with one button its useful to have a table.

Re: love.mousepressed how does it work?

Posted: Sun Sep 22, 2013 11:11 pm
by Fatmat927
Robin i dont understand why the +width and the +height also is it it really a "_" for button x and button y?
but ill try it

Re: love.mousepressed how does it work?

Posted: Sun Sep 22, 2013 11:27 pm
by DaedalusYoung
As he wrote, that's just example code, just adding that won't work.

You need to add width and height of the button to test if the cursor is in the proper area. Consider your button is a square that starts at position 10, 10. How do you know if your mouse is over that button when you left-click? Well obviously, if the cursor x is lower than 10 or the cursor y position is lower than 10, the cursor is not over the button, so left click does nothing.

But what if cursor x position is 30, is that over the button? You don't know, unless you know where the button ends. You need to know the width of the button to calculate that. So let's say the width is 15. Now we can calculate where the button ends. It starts at 10, and its width is 15. So it must end at 10 + 15 = 25. So if the mouse x is 30, then it is not over the button and left click again does nothing.

Re: love.mousepressed how does it work?

Posted: Mon Sep 23, 2013 12:03 am
by Fatmat927
ok thx man but what for the "_"??

Re: love.mousepressed how does it work?

Posted: Mon Sep 23, 2013 12:05 am
by DaedalusYoung
That depends on how you define the button. You can use button_x and button_y if you want, but you can call it whatever you like, as long as you use the same name throughout your code.

Re: love.mousepressed how does it work?

Posted: Mon Sep 23, 2013 12:13 am
by Fatmat927
but what exactly does it define is it the position of the x while pressing l or is it something else? and really thx man ur helping me alot im starting to understand that part :D