Event Cool downs

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
MrGrizzly
Prole
Posts: 7
Joined: Sun Aug 23, 2020 5:24 pm

Event Cool downs

Post 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
MrGrizzly
Prole
Posts: 7
Joined: Sun Aug 23, 2020 5:24 pm

Re: Event Cool downs

Post 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
sphyrth
Party member
Posts: 260
Joined: Mon Jul 07, 2014 11:04 am
Contact:

Re: Event Cool downs

Post 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
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 246 guests