Ben Harvey
Ben Harvey

Reputation: 49

How to tell if a key has been released in Love2d

I want to add an ability in my game where if you hold t then the enemies are slowed down. love.keyboard.isDown won't let me put the enemies back to their original speed once the t key has been released. Is their another way I could do this?

Upvotes: 0

Views: 888

Answers (3)

Alchemist
Alchemist

Reputation: 103

Using love.keyboard.isDown will let you put their original speed back if you check when it's false, like so:

if love.keyboard.isDown('t') then
    enemy_speed = 15
else
    enemy_speed = 30 -- 't' key has been released
end

but there is an another way to do this. Use love.keypressed and love.keyreleased, like so:

function love.keypressed(key)
    if key == 't' then
        enemy_speed = 15
    end
end

function love.keyreleased(key)
    if key == 't' then
        enemy_speed = 30 -- 't' key has been released
    end
end

Upvotes: 1

BukkitmanPlays MCPE
BukkitmanPlays MCPE

Reputation: 110

if I understand, love.keyboard.isDown("t") is for to the love.update() function, and it will just repeat the function no matter what. so in this case create a function like this in your main.lua file:

function love.keypressed(k)
    if k == "t" then
        // Code goes in here
    end
end

like this, it should activate once the key is pressed.

Upvotes: 0

Yu Hao
Yu Hao

Reputation: 122443

Use love.keyreleased.

Note that unlike love.keyboard.isDown, it's a callback function. Use it to register the action when the key t is released.

Upvotes: 1

Related Questions