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

我有一个函数可以让我在一行中添加和删除字符,但我想将其限制在大约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
点赞
用户2279620
用户2279620

你是否可以通过判断字符串是否过长来限制长度,如果过长就不添加进去呢? 还是你想要别的方式?

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
2014-02-28 17:29:31