Love 2D 摇杆输入管理

我正在将摇杆控制作为键盘控制的一种选择进行添加。简言之,任何键盘或摇杆输入都会通过虚拟控制类进行过滤,并返回一个字符串。这个字符串由主程序反应。这个系统可以正常工作,但我需要精细处理摇杆部分。对于键盘部分,我有代码区分love.keyboard.isDown和love.keyboard.wasPressed,这样,只有一次输入,如enter、jump和其他一次性输入才会被执行一次。

起初我使用了joystick.isGamepadDown,对人物奔跑很好用,但同样的过程在预游戏菜单上所选的选项上使用会导致enter被多次注册。我想love.joystick.pressed是处理这个的方法,但我无法理清正确的顺序。请有人指点一下吗?谢谢

在main.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

在Virtual Control.lua中

function MenuInputs()
    for k, joystick in pairs(love.joystick.getJoysticks()) do
        if joystick:isGamepadDown('back') then
            return 'pause'
        end
        if joystick:isGamepadDown('dpdown') then
            return 'down'
        end
        if joystick:isGamepadDown('dpup') then
            return 'up'
        end
        if joystick:isGamepadDown('dpleft') then
            return 'left'
        end
        if joystick:isGamepadDown('dpright') then
            return 'right'
        end
        if love.joystickpressed(joystick, 'a') or love.joystickpressed(joystick, 'start') then
            return 'enter'
        end
    end

    if love.keyboard.wasPressed('space') then
        return 'pause'
    end
    if love.keyboard.wasPressed('s') or love.keyboard.wasPressed('down') then
        return 'down'
    end
    if love.keyboard.wasPressed('w') or love.keyboard.wasPressed('up') then
        return 'up'
    end
    if love.keyboard.wasPressed('a') or love.keyboard.wasPressed('left') then
        return 'left'
    end
    if love.keyboard.wasPressed('d') or love.keyboard.wasPressed('right') then
        return 'right'
    end
    if love.keyboard.wasPressed('enter') or love.keyboard.wasPressed('return') then
        return 'enter'
    end
end
点赞
用户13850885
用户13850885

我遗漏了使用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
2020-09-06 04:47:11