Love 2D游戏杆输入管理

问题描述

我正在努力将操纵杆控件添加键盘控件的一个选项。简而言之,任何键盘或操纵杆输入都通过Virtual Control类进行过滤,并返回一个字符串。该字符串由主程序响应。该系统可以正常工作,但是我需要完善操纵杆方面的知识。对于键盘部分,我有用于区分love.keyboard.isDown和love.keyboard.waspressed代码,因此输入,跳转和其他一次输入仅作用一次。

我最初使用操纵杆isGamepadDown,这对于运行角色非常有用,但是用于预播放菜单中选定选项的相同过程导致回车多次注册。我以为love.joystick.pressed是处理此问题的方法,但我无法掌握正确的顺序。有人能指出我正确的方向吗?谢谢

在main.lua

var output = decodeQuotedPrintable(input);
output = utf8.decode(latin1.encode(input));

在Virtual Control.lua中

function love.load()
    love.keyboard.keyspressed = {}
    joysticks = love.joystick.getJoysticks()
    joystick1 = joysticks[1]
    joystick2 = joysticks[2]

    love.joystick.buttonspressed = {}
end

function love.keyboard.waspressed(key)
    return love.keyboard.keyspressed[key]
end

function love.joystickpressed(joystick,button)
    print(love.joystick.buttonspressed[button])
    return love.joystick.buttonspressed[button]
end

function love.update(dt)
    love.keyboard.keyspressed = {}
    for k,joystick in pairs(joysticks) do
        love.joystick.buttonspressed = {}
    end
end

解决方法

我错过了使用love.gamepadpressed在每次按下按钮时都插入该按钮下方的布尔值的关键步骤,然后使用创建的函数(gamepadwasPressed)返回布尔值。现在可以正常使用了。

function love.load()  
    love.keyboard.keysPressed = {}

    JOYSTICKS = love.joystick.getJoysticks()
    JOYSTICK1 = JOYSTICKS[1]
    JOYSTICK2 = JOYSTICKS[2]

    joystick1buttonsPressed = {}
    joystick2buttonsPressed = {}
end

function love.keypressed(key)
    if key == 'escape' then
        love.event.quit()
    end

    love.keyboard.keysPressed[key] = true
end

function love.keyboard.wasPressed(key)
    return love.keyboard.keysPressed[key]
end

function love.gamepadpressed(joystick,button) 
    if joystick == JOYSTICK1 then
        joystick1buttonsPressed[button] = true
    elseif joystick == JOYSTICK2 then
        joystick2buttonsPressed[button] = true
    end
end

function gamepadwasPressed(joystick,button)
    if joystick == JOYSTICK1 then
        return joystick1buttonsPressed[button]
    elseif joystick == JOYSTICK2 then
        return joystick2buttonsPressed[button]
    end
end

function love.update(dt)
    love.keyboard.keysPressed = {}
    joystick1buttonsPressed = {}
    joystick2buttonsPressed = {}
end