Skip to content
Tags

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
timer=0

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

But you can use tweens to do this more elegantly

--somewhere in your code
tween.delay(3,NextStep)

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
tween.delay(3,NextStep)

function NextStep()
    --all your code here, then at the end...
    tween.delay(3,NextStep)
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.

Advertisement

From → Programming

Leave a Comment

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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: