如何在love2d中创建文本框

我一直在尝试制作一个供用户输入数据的框,一旦用户点击框,就会弹出文本输入。这是我目前的情况,但点击框不会导致文本输入被添加到其中。问题在哪里?

function love.load ()
    txt1 = ""

    columnx = { 50, 160, 260, 375, 495, 600, 710 }
    columny = { 130, 230, 330, 440, 540, 640 }

     if show1 == true then
        function love.textinput(t)
            txt1 = t
        end
    end
end

function love.mousepressed (x, y)
    if
        x >= columnx[4] and
        x <= 435 and
        y >= columny[1] and
        y >= 145 then
        show1 = true
    end
end

function love.draw ()
    love.graphics.print(txt1, columnx[4], columny[1])
end
点赞
用户7114852
用户7114852

我猜,love.textinput() 回调函数是您要找的。但是,正如术语 'callback' 所示,它将由 LÖVE 引擎调用,而不是您的代码。每当用户在游戏运行时输入一些文本时,它都会被调用。因此,您需要将其放在 love.load() 函数之外。

在 love2d.org wiki 上有一个关于此的示例(较小的一个)。

至于您的示例,请将 love.textinput() 移出 love.load() 并添加一个 if 语句:

function love.load()
    txt1 = ""

    columnx = {50, 160, 260, 375, 495, 600, 710}
    columny = {130, 230, 330, 440, 540, 640}
end

function love.textinput(t)
    if (show1) then
        txt1 = txt1 .. t
    end
end

-- 您的其他代码。
-- 也许可以将其与 wiki 上的“backspace example”混合使用...

-- 但是,您还可能需要一些函数来在文本输入后将'show1'重新设置为 'false'。也许是这样的:
function love.keypressed(key)
    if (key == "return") and (show1) then
        show1 = false
    end
end

希望我能帮助您一点!

2016-11-23 11:22:43