How To Get Started With Actionscript: Adobe Flash Professional Cs5 Activity 5.1 Guide
How To Get Started With Actionscript: Adobe Flash Professional Cs5 Activity 5.1 Guide
1 guide
For more about object-oriented programming, see Programming ActionScript 3.0, “Object-oriented Programming in
ActionScript” (in Flash, select Help > Flash Help).
Parameters area
Actions toolbox
Script Assist
button
Script pane
Script navigator
In Script Assist mode, you can add ActionScript to your Flash document without writing the code yourself. You
select actions from the Actions toolbox and set the options for each action in the parameters area. You must know a
little about what functions to use to accomplish specific tasks, but you don’t have to learn syntax (the grammar of
ActionScript). Many designers and nonprogrammers use this mode.
One of the first things to learn is how to stop your movie at a certain spot. You will also learn how to send the
playhead to a particular frame in the movie.
Frame number
Event handling
The technique for specifying certain actions that should be performed in response to particular events is known as
event handling. Event handling consists of three important elements:
• The event source: Which object will trigger the event? For example, which button will be clicked, or which
Loader object is loading the image?
• The event: What is going to happen, what interaction do you want to respond to? Identifying the event is
important, because an object can trigger (and listen for) several events.
• The response: What action(s) do you want performed when the event happens?
When an ActionScript program is running, Adobe Flash Player just sits and waits for certain events to happen, and
when those events happen, the player runs the specific ActionScript code you’ve specified for those events. For the
program to know what events are important, you must create an event listener. An event listener is a function Flash
Player executes in response to specific events.
Adding an event listener is a two-step process:
• First, you create a function or class method for Flash Player to execute in response to the event. This function is
sometimes called the listener function.
• Second, you use the addEventListener() method to connect the listener function with the target of the event. The
addEventListener() function tells Flash what object to listen to, what event to listen for, and what function to
execute in response.
14. In the Actions toolbox on the left side of the panel, select
the CLICK property from the MouseEvent class
(Figure 12).
To find the CLICK property, open Flash.Events, open
MouseEvent, and open Properties.
15. In the Script pane, select the AddEventListener() line to
display the parameters for this method. Then, in the
parameters area, click in the Type field.
16. In the Actions toolbox, double-click the CLICK property.
Script Assist adds the property to your code as
MouseEvent.CLICK (Figure 13).
Figure 12 CLICK property in the Actions toolbox
Now your code will listen for a click on the button. To
tell the event listener how to respond when that click
occurs, you next create a function.
17. In the parameters area, type a name for your function in
the Listener field, such as startMovie. You can use any
name you like, but make sure the name of the function is
unique and contains no spaces (Figure 14).
The function name appears in the Script pane as you type.
You have named the function, but you haven’t created it
yet.
18. In the Actions toolbox, select the function keyword from
Language Elements.
To find the function keyword, expand Language
Elements. Then expand Statements, Keywords & Figure 13 CLICK property in the Script pane
Directives, and expand Definition Keyword.
19. Double-click the function keyword in the Actions
toolbox.
The code for creating a function appears in the Script
pane (Figure 15).
20. In the parameters area of the Script pane, type the name
of your function in the Name field. Figure 14 Function name in the Listener field
Note: The function name must be typed exactly as you
typed it for the AddEventListener function in step 17.
Function names are case-sensitive.
21. In the parameters area of the Script pane, type
event:MouseEvent in the Parameters field.
In this field, you are naming a variable (event) and
indicating what type of variable it is (MouseEvent).