A Lingo script is a series of one or more handlers. You can attach a script to any of the following elements of your Director movie:
A script attached to a sprite or frame is often referred to as a behavior script, or a Score script . A script attached to a cast member is a cast-member script. And a script attached to a movie is a movie script . (A more advanced type of script, called a parent script, is not covered in this book.) With the exception of cast-member scripts, every script appears as a cast member in the Cast window ( Figure 15.4 ).
Figure 15.4. The various kinds of script cast members as they appear in the Cast window.
The element to which a script is attached determines when and where the script is executed, as described below.
Behavior scripts are simply a different name for the behaviors you learned about in Chapter 14, "Adding Behaviors." As you saw in that chapter, you can create behaviors by using the Events and Actions pop-up menus in the Behavior Inspector ( Figure 15.5 ). You can achieve much more flexibility, however, by writing behaviors as Lingo scripts in a Script window ( Figure 15.6 ). In either case, once a behavior has been created, you use it by dragging it from the Cast window onto a sprite, or into a frame in the script channel of the Score.
Figure 15.5. The Behavior Inspector offers a menu-driven approach to creating behaviors.
Figure 15.6. Writing behavior scripts gives you much more control than using the Behavior Inspector.
You can attach multiple behavior scripts to one sprite, but you can attach only one script per frame in the script channel. If a sprite has multiple behaviors that respond to the same event, each of them is executed in the order in which you attached them. You can see which behaviors have been attached to a sprite ”and their order ”by selecting the sprite and opening the Behavior Inspector.
A behavior script attached to a sprite may be triggered by any of the following events:
A behavior script attached to a frame is generally triggered when the playhead prepares to enter, enters, or leaves the frame to which the script is attached (for example, on prepareFrame , on enterFrame , or on exitFrame ).
A cast member may have only one script attached to it, and a cast-member script may be attached to only one cast member. If a cast member has a script attached to it, the presence of that script is indicated by an icon in the lower-left corner of the cast member's thumbnail in the Cast window ( Figure 15.7 ).
Figure 15.7. A cast member with an attached script.
A cast-member script works the same way as a behavior script attached to a sprite. It can respond to any of the same mouse or keyboard events that a behavior script responds to. (It can't respond to movement of the playhead, however.)
A cast-member script remains attached to any sprite that's created from that cast member. So, for example, suppose you create a visual cast member and attach the following script to it:
on mouseDown go to frame 1 end
Any sprite based on that cast member will function as a button that returns the playhead to frame 1 when the user clicks it.
If you have a cast member that you want to use as a button, but you want the button to do different things at different points in the movie, don't attach a cast-member script to it. Instead, attach a behavior script to each sprite based on the cast member.
A movie script is not explicitly attached to any object; instead, it's available to the entire movie. Two types of handlers are generally placed in movie scripts: