Log In  

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.

P#103915 2021-12-31 16:03

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.)

P#103917 2021-12-31 16:19 ( Edited 2021-12-31 16:47)

(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.)

P#103922 2021-12-31 17:29

Thanks!

P#103923 2021-12-31 17:38

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-28 12:42:13 | 0.005s | Q:11