|Programming Art's Tutorials|
How to Build a Timer Object Using Lingo
Perhaps you're experimenting with object oriented programming for the first time, or maybe you're experienced in
another language in just need an example of how to do it in Lingo. Either way, the following tutorial should prove
useful for demonstrating how to create and use an object with Lingo and why it can be very useful. If you have something
to contribute [or contest], please post
your comments to the forums.
Check out the source movie with this example to get an even closer look at the timer object. Good luck!
Download the FREE Director source movie here! [Director
Why use objects?
Imagine a Director project you've created and you've made
a car that you can make move around the screen and basically
make behave like a car. Perhaps you've created global variables
that remember certain attributes about your car such as how
much gas it has, what gear it's in, it's velocity, direction,
etc. Your car works perfectly fine, but there is still a
problem. What if you need 10 cars moving around the screen
instead of 1? This is where objects come into the picture.
Instead of storing one set of global variables to remember
all the attributes of one car, you create a car object that
remembers these attributes for you. In addition to these
attributes, your car object also handles functions such as
shifting your car into different gears, accelerating, braking,
and reporting fuel usage, among many other things. The real
beauty of the situation is that you can create as many instances
of this car object as you like. So, having 10 cars on your
screen, remembering how much gas each one has, what speed
each one is traveling, and what direction each one is going,
is no longer a problem. In fact, you could have ANY number
of cars all doing different things and it would still be
easy for you to manage.
The Timer Object example
The following timer object demonstrates the use of objects
by showing how "the timer" in Director can be used to represent
an infinite number of independent timers.
First of all, start "the timer" in your startMovie:
Next, put the following code into a cast member all by itself
and give that member a name. If you name the member "timerObject",
then that is the name you'll use when creating an instance
of the timer later on.
property startedTime, additionalTime, isStarted
on new me
set additionalTime = 0
set startedTime = 0
set isStarted = FALSE
on ActivateTimer me
set the startedTime of me = the timer
set the isStarted of me = TRUE
on ReportTimer me
if (the isStarted of me = TRUE) then
timer - startedTime) + additionalTime)
on AddToTimer me, addTime
set the additionalTime of me = the additionalTime
of me + addTime
on DeactivateTimer me
set the isStarted of me = FALSE
set the additionalTime of me = 0
set the startedTime of me = 0
The first thing you see in the code is a list of properties.
These are attributes that will be remembered by your object.
After the properties, there are 5 handlers. These handlers
provide the functionality of your object. The "on new me"
handler is special and required in all object scripts. Every
time you create a new instance of an object, this special
handler will be called and it is the handler that returns
the object instance when creating a new object. The "return
me" line of this handler will be present in every object
script. Now, let's create an instance of this object:
set gTimer1 = new(script
gTimer1 is now a timer object instance and since we made
it a global variable, it can be accessed anywhere in your
program. Imagine a button that toggles between stopping and
starting a timer. Here is an example:
if the isStarted of
gTimer1 = FALSE then
"It's been "&ReportTimer(gTimer1)&" timer ticks since you
Now go get the source file and try it yourself!