love.mousepressed how does it work?

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.
Fatmat927
Prole
Posts: 42
Joined: Wed Sep 18, 2013 1:15 am

love.mousepressed how does it work?

Post 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 :/
User avatar
adnzzzzZ
Party member
Posts: 305
Joined: Sun Dec 26, 2010 11:04 pm
Location: Porto Alegre, Brazil

Re: love.mousepressed how does it work?

Post 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.
Fatmat927
Prole
Posts: 42
Joined: Wed Sep 18, 2013 1:15 am

Re: love.mousepressed how does it work?

Post 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?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: love.mousepressed how does it work?

Post 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.
Help us help you: attach a .love.
jjmafiae
Party member
Posts: 1331
Joined: Tue Jul 24, 2012 8:22 am

Re: love.mousepressed how does it work?

Post 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.
Fatmat927
Prole
Posts: 42
Joined: Wed Sep 18, 2013 1:15 am

Re: love.mousepressed how does it work?

Post 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
User avatar
DaedalusYoung
Party member
Posts: 407
Joined: Sun Jul 14, 2013 8:04 pm

Re: love.mousepressed how does it work?

Post 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.
Fatmat927
Prole
Posts: 42
Joined: Wed Sep 18, 2013 1:15 am

Re: love.mousepressed how does it work?

Post by Fatmat927 »

ok thx man but what for the "_"??
User avatar
DaedalusYoung
Party member
Posts: 407
Joined: Sun Jul 14, 2013 8:04 pm

Re: love.mousepressed how does it work?

Post 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.
Fatmat927
Prole
Posts: 42
Joined: Wed Sep 18, 2013 1:15 am

Re: love.mousepressed how does it work?

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

Who is online

Users browsing this forum: Google [Bot] and 4 guests