In this exercise you will learn how to make use of timeout objects to precisely control the duration of specific media elements in your Director movies.
By default, Director uses frame rate to control the speed at which events take place. Though the playhead seems tangible, as something that can be moved directly with a mouse in the Score, it is only a structural metaphor that allows you to arrange media content across time, and conveniently access the timeline of your movie as frames in the Score.
The timeline metaphor is effective for most work, but there are situations where the timeline gets in the way of productivity. Consider a movie that contains 300 images that are to be displayed one after the other. Using the timeline to construct a movie that allows the user to navigate through those images would be a tedious process. By using Lingo it can all be done within the same frame of the movie, and the layout of the slide sprites, even if there are 3000 of them, are a relatively simple task to manage. However, if you want slides to appear on screen for only a certain period of time the task becomes more challenging.
Using the Time Channel in the Score makes it easy to control the length of time a specific sprite remains on the screen, but how can the timing of images for a slideshow be controlled with Lingo? The answer is with something called a timeout object. A timeout object is defined in a script, and is considered a "script object." In the simplest terms, a timeout object works like a timer and sends a message when the timer expires. However, you may create as many timeout objects as you like, and each timeout object can have a unique frequency setting, so by using timeout objects you have extremely precise control of the timing of events, which are independent of the frame rate of the movie. Using a timeout object, any message can be sent at any millisecond interval you like.
Creating a Timeout Object:
Here's a simple timeout object that is created when the user clicks the mouse.
Note: The following timeout object is written for Director MX 2004 or later. Earlier versions of Director use a similar, but different syntax for timeout objects. The old syntax style is deprecated, so it is best to use the syntax style shown below.
myTimer = timeOut().new("myName", 3000, #moveCar, me)
Here's a breakdown of how the above timeout object works.
A - The timeout object is assigned to a variable. The variable does not need to be declared as a property or global variable. By assigning timeout objects to a variable you avoid what is called a memory leak, which is an undesirable situation where code continues to use more and more memory for itself, eventually using up all available memory on the computer (in an extreme case). Assigning the timeout object to a variable avoids a potential memory leak problem.
B - This is the beginning of the timeout object definition. It begins with the word timeout().
C - The new command is used to create a new instance of an object. In this case we are creating a new instance of a timeout object.
D - Every timeout object requires a unique name. The name is placed within quotes and is the name used to refer to the object for reference purposes. The name of this timeout boject is "myName".
E - This parameter indicates the frequency with which the timeout object should call the specified handler. This value is in milliseconds, so a frequency of 2 seconds would be written as 2000, 10 seconds as 10000, half a second as 500, etc. As written, this timeout object sends a message every 3 seconds.
F - This is the name of the handler or message that will be sent. The # sign must precede the name of the handler or message because it is a symbol.
G - This is an object reference and is not always needed.
Timeout objects are easy to create, but you have to remember to appropriately deactivate them, or you may be confused by having numerous timers sending messages. Once a timeout object is created it will run until it is told to stop. To stop a timeout object from running use the forget() command. For example, to cancel the above timeout named "myName" with a mouse click you would do this:
That covers the basics of timeout objects, but it takes some planning and thought to determine how a timeout will be created, then later stopped. One method that is easy to implement is to use frame scripts to create and stop timeout objects. For example, frame 1 of a movie would create a timeout object and frame 15 would have a script to stop the timeout. You can also use mouse events, such as a mouseUp to start a timer, and later have a frame script that stops it.
To see examples of how timeout objects can be used in your movies explore the two example movies for this exercise. The example movies are well-commented and include timeout-driven animation, and a self-running slide show that uses a timeout object for the pacing of each image.
Using the example movies as a starting point, create a Director movie that makes use of one or more timeout objects. Remember, you can create (instantiate) as many timeout objects as you like. Multiple timeout objects allow for very sophisticated animations and timed events that do not depend on the movie's frame rate or the playhead to operate.
Note: For example movies that demonstrate various timer techniques, search for timer or timeout in the search field, near the upper right corner of this site page.