2016-11-23 70 views
1

我一直在试图为用户输入数据后,用户单击该框。这是我迄今为止,但单击该框不会导致文本输入添加到它。问题在哪里?如何在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 

回答

5

你几乎在路上,但我会给你一些关于如何创建基本文本框的提示。

首先,将一个概念文本框设置为一个单一表格,其中包含需要了解其自身以正确更新和呈现的所有信息。推理自我包容的东西要容易得多。

文本框需要知道它的位置,大小,它是否处于活动状态以及它包含哪些文本。我们可以将它压缩成如下所示的表格。

local textbox = { 
    x = 40, 
    y = 40, 
    width = 400, 
    height = 200, 
    text = '', 
    active = false, 
    colors = { 
     background = { 255, 255, 255, 255 }, 
     text = { 40, 40, 40, 255 } 
    } 
} 

(我们还保存了一些颜色信息。)

的简单的方法来添加文字是通过love.textinput后,如您所见。在你的代码中,我们只检查文本框是否被激活一次,在love.load,这当然不是,因为我们可能还没有采取任何用户输入。与其试图重载函数,我们只是检查文本框是否在处理程序中处于活动状态,并相应地执行。

function love.textinput (text) 
    if textbox.active then 
     textbox.text = textbox.text .. text 
    end 
end 

我们已经介绍了如何检查用户是否点击了这个问题一个矩形区域内:Love2d cursor positions。我们想要停用文本框,如果它当前处于活动状态,并且用户在其空间的以外点击

function love.mousepressed (x, y) 
    if 
     x >= textbox.x and 
     x <= textbox.x + textbox.width and 
     y >= textbox.y and 
     y <= textbox.y + textbox.height 
    then 
     textbox.active = true 
    elseif textbox.active then 
     textbox.active = false 
    end 
end 

最后我们需要渲染我们的文本框。我们使用unpack来扩展我们的颜色表格,并使用love.graphics.printf来确保我们的文字在我们的文本框空间内环绕。

function love.draw() 
    love.graphics.setColor(unpack(textbox.colors.background)) 
    love.graphics.rectangle('fill', 
     textbox.x, textbox.y, 
     textbox.width, textbox.height) 

    love.graphics.setColor(unpack(textbox.colors.text)) 
    love.graphics.printf(textbox.text, 
     textbox.x, textbox.y, 
     textbox.width, 'left') 
end 

这些是创建一个非常粗糙的文本框的基本思想。这并不完美。请注意,您需要考虑文本在长度方面比我们最初设置文本框的高度时更长,因为这两者只是松散相关的。


使你的程序更易于阅读,更易于扩展,您在上面看到的一切真的应该放在自己的函数,处理文本框的表,而不是用公共代码弄乱love处理程序。看一看Programming in Lua的第16章,其中涵盖Object-Oriented Programming--游戏开发的一个重要主题。


请参阅love.textinput关于如何处理退格,删除字符的页面。


一些额外的事情要考虑:

  • 我们怎样才能从无效区分一个积极的文本框?
  • 我们如何创建一个文本框列表,所以我们可以在屏幕上有多个(但只有一个活动)?
+0

谢谢这有助于很多。 – laquishabonquiquithe3rd

+0

每件事看起来都是正确的,并且有道理,但每当我点击框并尝试输入一个字母/数字,它都不起作用。我也没有其他的错误。 – laquishabonquiquithe3rd

+1

@ laquishabonquiquithe3rd您可以创建一个完整的,但最小的'main.lua'文件,并将其托管在https://gist.github.com之类的位置,以便我可以看一下吗? 'love.textinput'应该像广告一样工作。你正在使用哪个版本的爱? – Oka

1

我想,love.textinput()回调函数是你在找什么。但是当术语'回调'意味着,它将被LÖVE引擎调用,而不是您的代码。只要用户在游戏运行时输入一些文本,就会调用它。因此您需要将其放在love.load()函数之外。
在love2d.org维基是一个例子(较低的一个)。

至于你的榜样,移动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 

-- The rest of your code. 
-- And maybe mix it with the 'backspace example' from the wiki... 

-- But you also might want to have some function to set 'show1' back to 'false' after the text input. Maybe something like this: 
function love.keypressed(key) 
    if (key == "return") and (show1) then 
     show1 = false 
    end 
end 

我希望我可以帮你一点!