lua - 限制输入文本的字符 - love2d

时间:2014-02-28 15:52:05

标签: input lua character limit love2d

我有一个功能,允许我添加和删除一行中的字符我希望将其限制为大约10个字符

function love.keypressed(key, unicode)
    if key == "backspace" or key == "delete" then
        player = string.sub(player, 1, #player-1)
    elseif unicode > 31 and unicode < 127 then
        player = player .. string.char(unicode)
    end
end

1 个答案:

答案 0 :(得分:1)

如果它太长,你不能通过不添加字符串来限制长度吗? 或者你是在追求其他东西吗?

function love.keypressed(key, unicode)
    if key == "backspace" or key == "delete" then
        player = string.sub(player, 1, #player-1)
    elseif unicode > 31 and unicode < 127 and #player <=10 then
        player = player .. string.char(unicode)
    end
end