lua - Love 2D 摇杆输入管理
问题描述
我正在努力添加操纵杆控制作为键盘控制的选项。简而言之,任何键盘或操纵杆输入都会通过 Virtual Control 类进行过滤并返回一个字符串。该字符串由主程序作出反应。该系统工作,但我需要巧妙地操纵操纵杆方面。对于键盘部分,我有区分 love.keyboard.isDown 和 love.keyboard.wasPressed 的代码,因此输入、跳转和其他一次性条目只执行一次。
我最初使用的是操纵杆isGamepadDown,这对于角色运行来说很好,但是用于预播放菜单上的选定选项的相同过程导致输入多次注册。我在想 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
解决方案
我错过了使用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
推荐阅读
- python-3.x - python中sorted()函数背后的逻辑
- flutter - 在 Flutter 中使用 Dio 包下载多个文件
- flutter - 如何将 Flutter 应用设置为默认短信应用?
- javascript - 如何不对所有选择激活选择功能
- swift - 按钮stackView间距不起作用-Swift-以编程方式
- c# - 通过 lambdada 表达式在组中选择当前记录
- mysql - 授予用户对存储过程的远程访问权限
- php - 如何将带逗号的数字转换为货币格式
- php - 如何在 Codeigniter 中将所有数据从一个数据库克隆到另一个数据库
- docker - 我可以使用 Docker 桥接网络模式将容器连接到 Cassandra 实例吗?