love.mousepressed how does it work?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
love.mousepressed how does it work?
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?
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.
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?
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?
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: love.mousepressed how does it work?
You can check it like this: (just example code)
If you have more than one button, you'd usually want to use a table to keep track of the buttons.
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
Help us help you: attach a .love.
Re: love.mousepressed how does it work?
even with one button its useful to have a table.Robin wrote:You can check it like this: (just example code)If you have more than one button, you'd usually want to use a table to keep track of the buttons.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
Re: love.mousepressed how does it work?
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
but ill try it
- DaedalusYoung
- Party member
- Posts: 413
- Joined: Sun Jul 14, 2013 8:04 pm
Re: love.mousepressed how does it work?
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.
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?
ok thx man but what for the "_"??
- DaedalusYoung
- Party member
- Posts: 413
- Joined: Sun Jul 14, 2013 8:04 pm
Re: love.mousepressed how does it work?
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?
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
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot] and 3 guests