Is there a way to detect when time() changes? I’m trying to make a movement system where when a button is pressed a sprite moves for a certain amount of time.
You just need to make a variable which stores the value of time() at the point the button was pressed. Then you can use that value for comparison against a new call to time().
Something like:
function _init() p={x=0,y=60,s=1,t=time()-8} end function _update() if time()-p.t<2 then p.x+=.5 else if(btn(🅾️))p.t=time() end end function _draw() cls() spr(p.s,p.x,p.y) end |
(Edit: It's unlikely, but if your program is running for more than 9 hours, note that: "PICO-8's number type has an upper limit of 32767.9999 before it wraps back to -32768. This means the values from time() may become problematic after roughly 9 hours and 6 minutes." Source: Pico-8 wiki.)
(re: time() wrapping around if a program runs for more than 9 hours: I think it should be fine if you're just looking at differences? Like, I just checked, and if x=-32767 and y=32767, then x-y=2, as you'd hope.)
[Please log in to post a comment]