Love 2D 摇杆输入管理
2020-9-5 22:40:19
收藏:0
阅读:140
评论:1
我正在将摇杆控制作为键盘控制的一种选择进行添加。简言之,任何键盘或摇杆输入都会通过虚拟控制类进行过滤,并返回一个字符串。这个字符串由主程序反应。这个系统可以正常工作,但我需要精细处理摇杆部分。对于键盘部分,我有代码区分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
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

我遗漏了使用
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