0
\$\begingroup\$

I want to be able to divide the window of the game in, lets say, 3 places, and when the mouse (or touch) click one of those places, execute some code.

Do I have to make each portion a button-like object? or is there a way for me to use love.mouse.getPosition() and give different instructions to specifics range of coordinates?

\$\endgroup\$
0
\$\begingroup\$

Let's say you want to figure out if the user clicked/touched the left or right side of the window, and that your window is 800 units wide. Then the code is very simple; in your click handler (typically love.mousepressed()) check if the x position of the mouse is less than or greater than half that, 400:

function love.mousepressed(x, y, button, istouch)
   if button == 1 then
      if x < 400 then
          -- left side clicked
      else
          -- right side clicked
      end
   end
end

But if you're doing anything more complex than that, like a GUI, this quickly becomes unmaintainable. Think about adjusting those GUI elements, adding new buttons, resizing them, changing the layout, and calculating those magic numbers... yuck! Then it's best to look at a proper GUI library, or code your own. But under the hood the library will be doing the same thing - checking the mouse press against coordinate numbers.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.