Creating a Pause with Lingo
As we mentioned, Lingo is Director's scripting language, and it's capable of programmatically controlling nearly every aspect of the application. Lingo has evolved into a very robust, fast, object-oriented language that is easy to learn but hard to master. A very cool feature introduced with Director 6.5 is behaviors. The great thing about behaviors is they immediately expose you to object-oriented programming in a very natural manner.Let's create a simple behavior that pauses the playhead, and then instantiate it multiple times to allow pausing at each section in the movie.
|2. ||In the script panel, complete the behavior that Director started for you, and name your script loop_on_frame.Director will automatically begin a script for you if you create the script on a frame or a sprite. If created on a frame an exitFrame handler will be started, as is the case here. Like many of the newer development applications, Director uses an event-based system. Events are just things that happensuch as a keystroke, a mouse click, or in this case an exit frame. Some events, like the exit frame, are generated by Director during the normal course of movie playback. Other events, such as a mouse click, are generated by the user. You write handlers to capture and handle the eventa handler begins with the keyword on. If no handler exists for an event, it simply disappears. It's actually a tiny bit more complicated than that, but this is all you need to know for the time being.Enter line 2 to complete the default exitFrame handler:|
1 on exitFrame me
NoteAs stated in the Elements and Format section of the Introduction, code line numbers are there for identification purposes only and are not to be entered.Don't forget to give the script its name.
|3. ||In the Script Panel's toolbar press the lightning bolt, which recompiles all scripts. When you recompile, Director takes the code you wrote and compiles it into a form readable by the computer.TIPIf you don't see the lightning bolt in the toolbar, you may need to expand the script window horizontally to be able to see all of the buttons available.Can you tell what line 2 does? As Director plays through each frame of your movie it generates a few different events, such as enterFrame and exitFrame. These are generated on each frame as the movie plays. So when the playhead reaches frame 10 and Director generates the exitFrame event, your on exitFrame handler will catch it and execute line 2. In plain English, line 2 says, "Hey movie, go to the frame I'm on right now."If you wanted to go to frame 20 you could have said _movie.go(20). Instead, _movie.frame is a movie property that contains the frame the playhead is currently on. This will allow you to use this ehavior on any frame of the movie, instead of having to write separate scripts for each place you want to pause.Close the script panel. You will note that cast member 6 has now been created. The little yellow flower-like icon means the member is a behavior:|
|4. ||Play the movie.When the playhead reaches frame 10, it will pause. However, it's not actually paused as would happen if you used the tempo channel's wait options. Instead each time the playhead tries to leave the frame, the exitFrame event is generated, and captured by the script, which sends the playhead back to the frame it's already on. In this manner the movie continues to play, but in a single frame, and will not freeze Lingo or other objects like using the Tempo channel would.|
|5. ||From the cast, drag the loop_on_frame script and drop it onto frame 34 in the Score's script channel, at the last frame of the project1 section. Repeat, but this time drag the script to frame 59the last frame of the project2 section.The behavior channel, in your Score, should now appear like the following, with the script you just created used in frames 10, 34, and 59:[View full size image]It seems natural, doesn't it? You created one general behavior script and used it on three different frames. In some languages, the script you created would be called a class, and using it in three different spots is known as instantiating it. If you edit the script, it changes in every location it's used. This is just a taste of what you can do using behaviors, as you will learn in later lessons.I should also mention that while behaviors are somewhat like classes, they are specialized forms of classes that are attached to sprites and frames only. Lingo does have a more formal definition of a class, called a parent script, that you will learn about when you do the third project in the book: the 2D game.Right now, if you played the movie it would play until it reached one of the scripts, pause, and do nothing more. So our next step is to start working on the buttons that will send the movie to the appropriate section.This is also a good spot to again save your progress, so press Ctrl/Cmd+S.|