Skip to content

100. Timers (revisited)

August 3, 2013

There is an interesting forum post here, about timers, showing how tweens can be used.

Suppose we want to wait 1 second and then run the function NextStep.

The “normal” approach would be to use a counter, like this.

--somewhere in your code

--then in the draw function
timer=timer + DeltaTime --add time since last redraw
if timer>1 then 

But you can use tweens to do this more elegantly

--somewhere in your code

Tween.delay works in exactly the same way as a counter, but is more elegant.

Repeating timers

If you want a repeating timer, eg something that happens every 3 seconds, you can call tween.delay again, after it finishes, ie

--somewhere in your code

function NextStep()
    --all your code here, then at the end...

What is also nice is that if you are using a counter of your own, and testing its value in the draw function, when the counting is done, you need to turn it off so draw doesn’t keep using it. Tween handles all this for you, automatically shutting down the counter at the end.

Very neat.

It’s not going to work for everything – measuring things like frames per second (speed) is still best done with your own counters – but it is a useful tool to know about.


From → Programming

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: