Page 1 of 1

Event Cool downs

Posted: Tue Aug 25, 2020 7:47 pm
by MrGrizzly
I've been trying to make a game in which a character shoots a projectile. A problem I have is that bullets can be spammed can be shot right after one another so I wanted to make it so between each shot there is a 2 second cool down. I've attempted at creating a cool down and have succeeded but not in the way I want to. After pressing space the bolt shoots and starts the cool down. Sadly, after the cool down ends it immediately fires again without waiting for space to be pressed again when I want to to wait for space to be pressed again.

Here's my code:
function love.load()
cooldown = 0
end

function love.update(dt)
cooldown = math.max(cooldown - dt,0)
if love.wasReleased('space') and cooldown == 0 then
player1Bolt:reset()
cooldown = 2
player1Bolt.dx = BOLT_SPEED
end
end

Re: Event Cool downs

Posted: Tue Aug 25, 2020 7:52 pm
by MrGrizzly
And here's my wasReleased function and keysReleased function

function love.keyboard.wasReleased(key)
if (love.keyboard.keysReleased[key]) then
if love.keyboard.isDown(key) then
return false
else
return true
end
else
return false
end
end

function love.keyreleased(key)
love.keyboard.keysReleased[key] = true
end

Re: Event Cool downs

Posted: Wed Aug 26, 2020 1:17 am
by sphyrth
I think moving your "fire bolt" code to love.keypressed function would solve the problem:

Code: Select all

function love.load()
  cooldown = 0
end

function love.update(dt)
  cooldown = math.max(cooldown - dt, 0)
end

function love.keypressed(key)
  if key == 'space' and cooldown == 0 then
    player1Bolt:reset()
    cooldown = 2
    player1Bolt.dx = BOLT_SPEED
  end
end