Game Development Reference
In-Depth Information
The timer comes to our rescue. Though the word timer might bring to mind something like an alarm
that goes off at the end of a specified time, in Lua, the timer is more like a heartbeat—one that we can
control the beat of. And we can decide what we want the program to do when that timer beats.
Setting up a timer is easy—we simple use the following:
local heartbeat = timer.performWithDelay(1000,
function()
print("tick")
end, 0)
When you run this, you'll see the words tick outputted to the terminal once per second. The function
Timer namespace and has four functions: timer.performWithDelay() to create a new
timer.cancel() to cancel a timer, timer.pause() to pause the timer from firing further, and
to continue from the point where the execution was paused.
performWithDelay takes a single parameter, which is the handle to
performWithDelay , when called, returns a handle to
delay , listenerFunction ,
iterations . The first parameter, delay , specifies the frequency that the timer event is fired in
listenerFunction ; this points to a Lua function and is the
Here's how to print a list of numbers from 1 to 10 using the timer:
local counter = 1
function printNumber(event)
print("Counter is :".. counter)
counter = counter + 1
end
timer.performWithDelay(1000,printNumber, 10)
Frames
While the workings of televisions and movies may seem complex, the way they work is actually quite
simple. The movie or TV screen displays a series of static images in rapid succession, fast enough that
our eyes perceive these images as continuous movement. This is also how animators create animation.
They draw a series of frames with the images, and when the images are then played back to us in rapid
succession, it provides us the illusion of movement. If the images are played back too slowly, we can
catch the changing of images between frames; if they're played too fast, everything appears abnormal
and sped up. Now, what does that have to do with development? Everything!
Most animation software works with a timeline. The timeline is generally the blueprint of the
animation, which determines the position and transformations of the objects over time or frames.
The app starts at frame 1 and iterates through a set number of frames to provide the illusion of
movement, till it reaches the end of the frames, when it stops. However, in some cases, the timeline
may wrap —that is, start again from the beginning as if in a loop. The numbers of frames that are
displayed in a second is referred to as the frame rate . The more frames you can display in a second,
 
Search WWH ::




Custom Search