freeve4h
freeve4h

Reputation: 75

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?

Upvotes: 0

Views: 777

Answers (2)

koyaanisqatsi
koyaanisqatsi

Reputation: 2813

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

Upvotes: 1

Luke100000
Luke100000

Reputation: 1539

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)

Upvotes: 2

Related Questions