'love2d key detected as being pressed more than once
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?
Solution 1:[1]
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)
Solution 2:[2]
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:
- Push the key D or R => Nothing happens
- Release the key D or R => Function is triggered
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Luke100000 |
| Solution 2 |
