检测到 love2d 键被多次按下

love2d key detected as being pressed more than once

所以我在制作游戏,运行 遇到了一个错误,其中 love.keyboard.isDown() 检测到一个键被按下了不止一次

代码:

function love.update(dt)
    if love.keyboard.isDown("escape") and Menu == false then
        Menu = true
    elseif love.keyboard.isDown("escape") and Menu == true then
        Menu = false
    end

当按下esc键时,游戏会在菜单和游戏之间疯狂切换。有没有办法避免这种情况?

love.keyboard.isDown(key)检查是否有按键被按下,即使是短按也通常是几帧。

看看https://love2d.org/wiki/love.keyboard.isDown

在您的情况下,您可能需要 love.keypressed 事件,它只触发一次 (https://love2d.org/wiki/love.keypressed)

有时需要这种行为,有时不需要。
如果您不想要这种行为,只需使用...

local debug = false

function love.keyreleased(key)                                                                                                                                      
    if key == 'd' then                                                                                                                                              
      if debug then debug = false else debug = true end                                                                                                             
    end                                                                                                                                                             
    if key == 'r' then                                                                                                                                              
      love.event.quit('restart')                                                                                                                                    
    end                                                                                                                                                             
end

行为:

  1. 按下按键 DR => 没有任何反应
  2. 松开按键DR => 触发功能

看:https://love2d.org/wiki/love.keyreleased