Lua 爱2D游戏杆输入管理

Lua 爱2D游戏杆输入管理,lua,game-development,love2d,joystick,Lua,Game Development,Love2d,Joystick,我正在努力添加操纵杆控制作为键盘控制的一个选项。简而言之,任何键盘或操纵杆输入都会通过虚拟控件类进行过滤并返回字符串。此字符串由主程序响应。该系统工作,但我需要巧妙的操纵杆方面。对于键盘部分,我有区分love.keyboard.isDown和love.keyboard.wasPressed的代码,因此enter、jump和其他一次性输入只作用一次 我最初使用的是操纵杆,isGamepadDown,这对于角色运行来说很好,但是用于播放前菜单上选定选项的相同过程导致enter注册了多次。我认为lov

我正在努力添加操纵杆控制作为键盘控制的一个选项。简而言之,任何键盘或操纵杆输入都会通过虚拟控件类进行过滤并返回字符串。此字符串由主程序响应。该系统工作,但我需要巧妙的操纵杆方面。对于键盘部分,我有区分love.keyboard.isDown和love.keyboard.wasPressed的代码,因此enter、jump和其他一次性输入只作用一次

我最初使用的是操纵杆,isGamepadDown,这对于角色运行来说很好,但是用于播放前菜单上选定选项的相同过程导致enter注册了多次。我认为love.mogage.pressed是处理这个问题的方法,但我无法得到正确的顺序。谁能给我指一下正确的方向吗?谢谢

总而言之,卢阿

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

我错过了使用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

我注意到了一些事情。一个是,您从不调用
MenuInputs()
函数。除非有东西说它会被自动调用,或者其他东西会调用它,否则我认为您可能需要在
love.update()中调用它。另一个原因是你的
love.update()
看起来毫无用处。看起来你在里面所做的就是将一些属性设置为等于一个空对象,并且每次游戏更新时你都会这样做。我不认为这样做有任何意义。只是澄清一下-我只包含了最少量的代码。有多种状态可以调用MenuInputs(),对于需要“isDown”的键盘和任何操纵杆命令,它都可以正常工作。我只是需要帮助理解为什么“joystickpressed”返回零。谢谢。它也在打印中打印零吗