Text Love2d-如何使文本可点击

Text Love2d-如何使文本可点击,text,lua,mouse,clickable,love2d,Text,Lua,Mouse,Clickable,Love2d,我有一个简单的文本,我希望它退出时,我点击文本。对不起,新来的love2d quit = love.graphics.print( "Quit", 450,375) function love.mousepressed(quit) love.event.quit() end 函数love.update(dt) 功能爱。鼠标按下(x,y) 如果x>440,x380,y=quit.x和x=quit.y和y会发生什么?你的鼠标垫有人打电话吗?event.quit()是否执行?抱歉,我回答了。我

我有一个简单的文本,我希望它退出时,我点击文本。对不起,新来的love2d

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

function love.mousepressed(quit)
  love.event.quit()
end
函数love.update(dt)
功能爱。鼠标按下(x,y)
如果x>440,x<540,y>380,y<410,则
love.event.quit()
结束
结束
结束

您可能希望创建一个对象,而不是使用
love.graphics.print
。然后,您可以在支票中查询其和,并使用
love.graphics.draw
显示它。代码可能如下所示:

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
函数love.draw()
love.graphics.draw(quit.text、quit.x、quit.y)
结束
函数love.load()
local font=love.graphics.getFont()
退出={}
quit.text=love.graphics.newText(字体“quit”)
退出。x=450
退出。y=375
结束
函数love.mousepressed(x、y、按钮、iTouch)

如果x>=quit.x和x=quit.y和y会发生什么?你的鼠标垫有人打电话吗?event.quit()是否执行?抱歉,我回答了。我真傻,警告!这重新定义了爱。每一帧都有鼠标按下。保持回调平坦,不要嵌套它们。
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