2014-02-27 26 views
2

我有一個簡單的文本,我希望它在我點擊文本時退出。對不起新love2dLove2d - 如何使文本可點擊

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

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

會發生什麼情況?你的mousepressed被調用了嗎? event.quit()是否執行? – Schollii

+0

對不起,我回答了。我當時很蠢 – Ross

回答

0
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 
+0

警告!這重新定義了每一幀「love.mousepressed」。保持回調平穩,不要嵌套它們。 – Robin

1

您可能需要創建的,而不是使用love.graphics.print一個Text對象。然後您可以在您的支票中查詢其widthheight,並使用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