Download (direct link):
traceC'If music be the food of love, play on");
Note Technically, the value between the parentheses of a statement such as trace() does not
need to be a quoted value, as in the previous example. However, it does need to evaluate to a string. You can find more discussion of this topic in Chapter 5 when variables and datatypes are discussed.
Now that we’ve looked at the trace() statement, you may be wondering where this statement goes so that Flash will do something with it. At this point you have the statement ready to go, but you need to actually “speak” it to Flash to get Flash to do what you want — which is to display the message in the Output panel.
The most fundamental technique for adding ActionScript code to a Flash movie is to use the Actions panel. We’ll examine this panel in much more detail later in this chapter (see “Understanding the Actions Panel”). For the purposes of getting up and running with ActionScript in this example, simply complete the following steps. You’ll read about the theory in more depth in just a moment.
1. Open a new Flash document.
2. Select the first keyframe of the default layer of the main timeline.
3. Open the Actions panel by choosing Window O Developer Panels O Actions or by pressing F9.
4. The right portion of the Actions panel is the Script pane. Type the following code into the Script pane:
trace("If music be the food of love, play on");
5. Test the movie by choosing Control O Test Movie or by pressing Ctrl+Enter (Windows) or ^+Enter (Macintosh).
When you’ve tested the movie in this way, you should see the Output panel open and display the following:
If music be the food of love, play on
Tip If the Output panel does not open and display the message, make sure that trace()
actions have not been omitted. You can do this by choosing File O Publish Settings. In the m Publish Settings dialog box, choose the Flash tab, and make sure that Omit trace actions is not checked.
68 Part II ♦ Laying the ActionScript Foundation
Understanding the Event Model: How ActionScript Works
In the simplest form, ActionScript can be viewed as simple commands, called statements, given to the Flash player. This is not unlike giving commands to a trained dog. The difference is that (one hopes) Flash responds the same way to the same commands with consistency, whereas Rover might not be so easily persuaded to sit or roll over when he has the idea of chasing the mail carrier.
It is also important to understand the bigger picture within which ActionScript works. One of the most important things to understand in Flash with respect to ActionScript is the concept of events and event handlers.
Events are those things that occur and can trigger another action or actions to happen. An event handler, on the other hand, can catch and process the event. Therefore, an event can occur independently, whether or not an event handler exists. And an event handler can exist independently of the occurrence of an event. However, without an event to trigger the event handler to respond, the event handler merely sits dormant, so to speak. It is much like pushing a button on the outside of a house to ring a bell on the inside. Pushing the button (the event) does nothing as long as there is not a bell (event handler) waiting to ring (action) inside. And the bell inside does not ring until the button is pushed. Here is another analogy to help you better understand this concept. An answering machine sits and waits until someone calls the phone line. The answering machine does nothing but sit there listening until the phone line is called. The answering machine represents the event handler. The call represents the event. And the answering machine recording a message represents the action that occurs when the event handler handles the event.
In Flash, the events can be grouped into several categories. The two most common types of events are what we’ll call time-based and user-based. The most common time-based example is that of the playhead entering a new frame. Each time this happens, it is an event. User-based events include mouse and keyboard activity.
The event handlers in Flash are those things equipped to handle specific events. Just like a lock and key, the event handlers accept only the events they are explicitly designed to handle. When you place your desired actions within the context of that event handler, they can execute when that event occurs. For example, if you create code within an event handler that handles mouse clicks, a keystroke entered by the user will never trigger that code. But if the mouse is clicked, the event is handled, and the code is executed.
As just discussed, Flash needs all actions to be placed within event handlers. There are two basic types of event handlers — keyframes and event handler methods. When you place ActionScript code on a keyframe, it is executed when the playhead enters the frame. When you place code within an event handler method, the code is executed when the corresponding event occurs. There are many types of event handler methods, as you’ll see in the section “Event Handler Methods.”