Placing a delay between key presses?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- BoopDeePoop
- Prole
- Posts: 39
- Joined: Sun Dec 30, 2012 4:15 am
Placing a delay between key presses?
I'm attempting to place delay between when you can press a certain key. I want it so that when you press the space bar, the key does what it's assigned to do, and in order for the game to take any more input from the space bar, you must wait x amount of seconds before it will actually press space again. I'm trying not to use any libraries for this so if someone could help that'd be great. Everytime I try nothing works.
Re: Placing a delay between key presses?
Try a timer.
Code: Select all
local time = 0
local enabled = true
function love.update(dt)
time = math.max(0, time - dt)
if time <= 0 then enabled = true end
end
--when you do your keypress:
time = delay
Re: Placing a delay between key presses?
Remember in the above example you'd also need to set enabled to false in the keypress, and you'd have to check if enabled == true before executing the keypress code.
- BoopDeePoop
- Prole
- Posts: 39
- Joined: Sun Dec 30, 2012 4:15 am
Re: Placing a delay between key presses?
I get main.lua 40: attempt to perform arithmetic on global "time" (nil value)
Here's my .Love
Here's my .Love
- Attachments
-
- game.love
- (208.93 KiB) Downloaded 193 times
- BoopDeePoop
- Prole
- Posts: 39
- Joined: Sun Dec 30, 2012 4:15 am
Re: Placing a delay between key presses?
I usually always put variables into load(). Guess I shouldn't then. Still, the error is gone now but I'm still able to press the space bar as many times as I want.
Code: Select all
function control_list(key)
--Only affects game if in 'Playing' state
if gamestate == "playing" then
if enabled == true then
if key == " " then
time = 3
enabled = false
phrase = math.random(1,20)
end
end
end
--Only affects game if in 'Menu' state
if gamestate == "menu" then
end
--Only affects game if in 'Options' state
if gamestate == "options" then
end
--General Controls, work in any state.
if key == "escape" then
love.event.quit()
end
if key == "1" then
gamestate = "menu"
elseif key == "2" then
gamestate = "playing"
elseif key == "3" then
gamestate = "options"
elseif key == "4" then
gamestate = "controls"
end
end
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Placing a delay between key presses?
You can, just don't declare them as local in love.load().BoopDeePoop wrote:I usually always put variables into load(). Guess I shouldn't then.
Help us help you: attach a .love.
- BoopDeePoop
- Prole
- Posts: 39
- Joined: Sun Dec 30, 2012 4:15 am
Re: Placing a delay between key presses?
Thanks for the note! I'm still having a problem with the code though. I'm able to press the key as many times as I want without delay.Robin wrote:You can, just don't declare them as local in love.load().BoopDeePoop wrote:I usually always put variables into load(). Guess I shouldn't then.
Re: Placing a delay between key presses?
make time = 0 global ( by removing local), that should fix it
Who is online
Users browsing this forum: Google [Bot], Semrush [Bot] and 4 guests