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
Event Cool downs
Re: Event Cool downs
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
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
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
Who is online
Users browsing this forum: Ahrefs [Bot] and 3 guests