Ross
Ross

Reputation: 313

Love2d - How to make text clickable

I have a simple text and I want it to quit when I click the text. sorry new to love2d

quit = love.graphics.print( "Quit", 450,375)

function love.mousepressed(quit)
  love.event.quit()
end

Upvotes: 2

Views: 1817

Answers (2)

Spencer
Spencer

Reputation: 4158

You might want to create a Text object instead of using love.graphics.print. You can then query its width and height in your check and display it using love.graphics.draw. The code might look something like this:

function love.draw ()
  love.graphics.draw(quit.text, quit.x, quit.y)
end

function love.load ()
  local font = love.graphics.getFont()
  quit = {}
  quit.text = love.graphics.newText(font, "Quit")
  quit.x = 450
  quit.y = 375
end

function love.mousepressed (x, y, button, istouch)
  if x >= quit.x and x <= quit.x + quit.text:getWidth() and
     y >= quit.y and y <= quit.y + quit.text:getHeight() then
    love.event.quit()
  end
end

Upvotes: 1

Ross
Ross

Reputation: 313

function love.update(dt)
    function love.mousepressed( x, y)   
        if x > 440 and x < 540 and y > 380 and y < 410 then 
            love.event.quit()
        end
    end
end

Upvotes: 1

Related Questions