Love2D键被发现不止一次

发布于 2025-01-26 03:55:50 字数 378 浏览 5 评论 0 原文

因此,我正在制作游戏,然后遇到一个错误,其中 love.kekeboard.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> ESC 键时,游戏在菜单和游戏玩法之间疯狂切换。有没有办法避免这种情况?

so i was making a game, and ran into a bug where love.keyboard.isDown() detected a key being pressed more than once

code:

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

when pressing the esc key, the game goes crazy switching between the menu and the gameplay. is there a way to avoid this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安静被遗忘 2025-02-02 03:55:50

love.keyboard.isdown(key)检查是否固定键,即使在短按短片中,这通常是几帧。

看看 https://love2d.org/wiki/wiki/love.key.keyboard.isboard.isdown 。

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

love.keyboard.isDown(key) checks whether a key is held down, which is usually a few frames even for a short press.

Take a look at https://love2d.org/wiki/love.keyboard.isDown.

In your case you probably want the love.keypressed event, which only triggers once (https://love2d.org/wiki/love.keypressed)

孤独患者 2025-02-02 03:55:50

有时这种行为是想要的,有时不是。
如果您不希望此行为简单地使用...

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. 按键 d r =>没有任何事情
  2. 会发布键 d r =>触发函数的

外观: https://love2d.org/wiki/wiki/love.keyreled.keyreled

Sometimes this behaviour is wanted and sometimes not.
If you dont want this behaviour simply use...

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

Behaviour:

  1. Push the key D or R => Nothing happens
  2. Release the key D or R => Function is triggered

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文