451 posts
Location
The left part of this post
Posted 19 May 2013 - 03:27 PM
function shold()
sleep(0.1)
timer = os.startTimer(0.1)
while true do
evt, _timer = os.pullEvent("timer")
if _timer==timer then
event = "sleep"
return
end
end
end
function _hold()
os.pullEvent("key")
os.pullEvent("key")
event = "key"
end
function hold()
pararell.waitForAny(shold, _hold)
if event=="sleep" then return false else return true end
end
Explanation:
function shold:
It sleeps 0.1 seconds to give _hold opportunity to start first
then it start a timer and waits till it finish
function _hold:
it detects two key presses and finish
function hold:
run imunstaniosly the two functions and return
This can be used to say how many seconds has been holded a key:
function shold()
sleep(0.1)
timer = os.startTimer(0.1)
while true do
evt, _timer = os.pullEvent("timer")
if _timer==timer then
event = "sleep"
return
end
end
end
function _hold()
os.pullEvent("key")
os.pullEvent("key")
event = "key"
end
function hold()
pararell.waitForAny(shold, _hold)
if event=="sleep" then return false else return true end
end
local start = os.clock()
while hold() do
--nothing
end
local end = os.clock()
print("You holded the key for "..start-end.." seconds!")
event = nil
587 posts
Location
Wrocław, Poland
Posted 19 May 2013 - 04:54 PM
It's not tutorial, It's code snippet…
At least put those comments in code
7508 posts
Location
Australia
Posted 20 May 2013 - 12:16 AM
It's not tutorial, It's code snippet…
That doesn't even work…..
Variable names are not allowed to be Lua keywords, you spelt parallel wrong ("pararell") and many other problems I can see with the code before even attempting to run it…
8543 posts
Posted 20 May 2013 - 12:58 AM
Please fix this and provide a write-up to go along with it. This isn't much of a tutorial at this point.
194 posts
Location
Spain
Posted 20 May 2013 - 02:43 PM
This line:
if event=="sleep" then return false else return true end
can be:
return event~="sleep"
Edit: The code I posted didn't work so I changed it, you can see why in the post below.
1688 posts
Location
'MURICA
Posted 20 May 2013 - 04:54 PM
This line:
if event=="sleep" then return false else return true end
can be:
return not event=="sleep"
That wouldn't work, it'd always evaluate to false. "not event" is false, and false is not equal to "sleep".
return event ~= "sleep"
I've actually done that mistake many times in the past and had my ass kicked by LÖVE because of it. :lol:/>
671 posts
Location
That place over there. Y'know. The one where I am.
Posted 20 May 2013 - 05:22 PM
Also, held*
194 posts
Location
Spain
Posted 21 May 2013 - 11:12 AM
That wouldn't work, it'd always evaluate to false. "not event" is false, and false is not equal to "sleep".
return event ~= "sleep"
I've actually done that mistake many times in the past and had my ass kicked by LÖVE because of it. :lol:/>
Yes, I don't know what I was thinking when I wrote that xd.
7508 posts
Location
Australia
Posted 21 May 2013 - 12:48 PM
Yes, I don't know what I was thinking when I wrote that xd.
Don't kick yourself too hard about it, you may have been thinking of this
return not (event == "sleep")
which would work fine. Although completely pointless given you have the ~=
222 posts
Location
Canada
Posted 11 July 2013 - 07:32 PM
Good snippet. I will remember how to detect if my keys are being holded, because 'held' is too mainstream.