When the visitor rolls into or out of the invisible button, the rollOver or rollOut event handlers will trigger the playing or stopping of the dog movie clip.. Working with Frame Events
Trang 1A function is a block of ActionScript code that
can be reused anywhere in a SWF file If you
pass values as parameters to a function, the
function will operate on those values A
func-tion can also return values Flash contains
built-in functions that let you access certain
information and perform certain tasks, such
as getting the version number of Flash Player
hosting the SWF file (getVersion()) Functions
that belong to an object are called methods
Functions that don't belong to an object are
called top-level functions and are found in the
Functions category of the Actions panel
Each function has its own characteristics,
and some functions require you to pass
cer-tain values If you pass more parameters than
the function requires, the extra values are
ignored If you don't pass a required
parame-ter, the empty parameters are assigned the
undefined data type, which can cause errors
when you export a script To call a function, it
must be in a frame that the playhead has
reached
To call a function, simply use the function
name and pass any required parameters The
following code describes a common syntax
for creating functions:
function firstFunction (x, y, z) {
// place all actions here;
}
Using Functions
Calling a Function
Functions begin with the word function, fol-lowed by the name of the function (user-defined) The area enclosed by parenthesis is used for passing parameters to the function actions If the parameters are left blank, you're essentially creating a generic function that will function the same way every time it's called If the function contains parameters, it will perform in a unique way each time it's called When you call a function, you're instructing Flash to execute all of the actions within that function Therefore, if firstFunction contained 20 actions, all of them would be executed by using a single line of script To call a function, simply add this line to the action:
myFunction ();
Passing Parameters to a Function
If the function has been defined to accept parameter information, you can use the fol-lowing line of script:
myFunction (parameter 1, parameter2);
Once a Function is defined, it can be called anytime it's needed Therefore, it's a good practice to define all of your functions in frame 1 of the active Flash document That way they can be called anytime after that
Trang 2Conditional statements in ActionScript are a
critical part of interactivity They let you
pro-gram a script based on any number of
condi-tions For example, in the morning, you say
good day, or good morning to someone you
meet In doing so, you made a conditional
choice
ActionScript does the same thing You can
create an ActionScript that checks the time of
day If it's before noon, Flash responds with a
Good Morning message If it's from noon to 5,
Flash says Good Afternoon, or from 5 till
mid-night, Flash says Good Evening This type of
condition is known as an if/else condition If
this happens, do this… else do that Since a
variable can be almost anything you can
measure on a computer, and a conditional
statement is made up of two or more
vari-ables, ActionScript can be taken to a point
where it almost thinks for itself The previous
example could be expressed in flow charting
the following way:
Typically, when you're creating a condi-tional statement, you're comparing one ele-ment against another using operators The following operators are available to create conditional statements:
◆ == Checks for equality between two values (is time of day equal to 5)
◆ != Checks for inequality between two values
◆ < Checks for less than (is value A less than value B)
◆ > Checks for greater than (is value A greater than value B)
◆ <= Checks for less than or equal to between two values
◆ >= Checks for greater than or equal to between two values
◆ && Checks for a logical AND (if day ==
"Friday" && time > 5)
◆ || Checks for a logical OR (if day ==
"Saturday" || day == "Sunday")
Using these operators to check between two or more values, you can create complex ActionScripts that react differently based on the available data To create a dynamic field that checks the time, and responds with the appropriate answer, you would enter the fol-lowing code:
if (time > "0000 && time < 1200) { response = "Good Morning";
} else if (time >1200 && time < 1700) { response = "Good Afternoon";
}else if (time > 1700 && time < 2400);
response = "Good Evening"
}
Using Conditional Statements
Trang 3Attaching a mouse event to a button is probably the easiest of all the event handlers For example, you have a movie clip of a dog that con-tains a barking audio file When the movie clip plays, the dog barks The trick is to have the dog bark when the visitor rolls their mouse over the dog’s face To do this, you will need to create an invisible button and then attach the mouse event to the invisible button
Attaching a Mouse
Event to a Button
Attach an Event to a Button
Click the Insert menu, and then
click New Symbol.
Select the Button type, and then
name the symbol
Click OK.
Create a blank keyframe in the Hit
state of the button, and then
create a shape
Leave the Up, Over, and Down
states blank
Exit the Symbol editing mode, and
then return to the Stage
Drag a movie clip onto the Stage
Create a new layer, and then name
the layer
Drag the invisible button onto the
Stage, and then place it over the
area of the image you want to use
as a button
Enter the script (ActionScript 2.0)
as shown in the illustration
◆ ActionScript 3.0 example files
are available on the Web at
www.perspection.com
When the visitor rolls into or out of
the invisible button, the rollOver or
rollOut event handlers will trigger
the playing or stopping of the dog
movie clip
Click the Control menu, point to
Test Movie, and then click Test.
10
9
8
7
6
5
4
3
2
1
7
8
9 6
3
2
Trang 4Frame event handlers are easy to understand When an action is attached to a frame, the action is triggered when the play head hits the frame For example, you want to create a frame event that swaps images on the Stage, and you want the images to swap every 40 frames You can attach an ActionScript that swaps the image, and place the action every 40 frames When the play head hits the frame, the action executes When you attach an ActionScript to a frame, you’ll need a blank keyframe on the Timeline, and it is strongly recommended that you always place ActionScripts in a separate layer from access and control In addition, if you're planning to swap images in a Flash movie, it's always best to use a blank movie clip (called a placeholder)
to hold the images
Working with
Frame Events
Attach an ActionScript
to a Frame
Drag a blank movie clip onto the
Stage, and then select the clip
Give the movie clip a unique
instance name in the Properties
panel
Create a new layer, and then name
the layer
Create blank keyframes at frame
numbers 1, 21, 41, and 61
Select a frame, click the Insert
menu, point to Timeline, and then
click Blank Keyframe.
Select frame 1, and then enter the
script (ActionScript 2.0) as shown
in the illustration
◆ ActionScript 3.0 example files
are available on the Web at
www.perspection.com
Select frames 21, 41, and 61, and
then repeat the script, except
change the name of the image you
want to load (image_b.jpg,
image_c.jpg, image_d.jpg)
Click the Control menu, point to
Test Movie, and then click Test.
8
7
6
5
4
3
2
1
6
4 3
2 1
Trang 5on(press) { trace("The button has been pressed.");
} You can specify two or more events for each on() handler, separated by commas The ActionScript in a handler executes when one
of the events specified by the handler occurs
For example, the following on() handler attached to a button will execute whenever the mouse rolls over or out of the button
on(rollOver, rollOut) { trace("mouse rolled in or out");
}
If you want different scripts to run when different events occur, you have the option to attach more than one handler to an object
You can attach onClipEvent() handlers to the same movie clip instance The first would exe-cute when the movie clip first loads (or appears on the Stage); the second executes when the movie clip is unloaded from the Stage
onClipEvent(load) { trace("loaded");
} onClipEvent (unload) { trace("unloaded");
If you’re working with ActionScript 2.0 (not
supported in ActionScript 3.0), you can attach
clip events to movie clips, which triggers an
action specified in the onClipEvent handler
Event handlers, also known as event
listen-ers, control when events in Flash occur When
you create a script, some event will be
invoked to trigger that particular action You
might want a specific movie clip to stop
play-ing when another movie clip loads on the
Stage, or when the user clicks or moves their
mouse
The Clip Event is one of a series of event
handlers that Flash uses to create actions
within a Flash movie You can attach event
handlers directly to a button or movie clip
instance by using the onClipEvent() or the
on() handlers The onClipEvent() handles
movie clip events, and on() handles button
events To use an on() or onClipEvent()
han-dler, attach it directly to an instance of a
but-ton or movie clip on the Stage, and then
specify the event you want to handle for that
instance For example, the following on()
event handler executes whenever the user
clicks the button the handler is attached to
Working with Clip Events
Assigning a Clip Event using the Behaviors panel.
Assigning a Clip Event directly in the Actions panel.
Trang 6For ActionScript 2.0, you can only attach an onClipEvent() to a movie clip instance that has been placed on the Stage You can't attach an onClipEvent() to a movie clip instance that is created at runtime; for example, using the attachMovie() method However, you can still attach multiple event handlers Using different event handlers within the same Flash document do not conflict with each other You could have a but-ton with an on(press) handler that tells the SWF file to play, and the same button can have an onPress method, for which you define a func-tion that tells an object on the Stage to rotate When the button is clicked, the SWF file plays, and the object will rotate Being able to consolidate different event handlers with a single instance gives you greater control, as well as less Stage clutter
Attaching a Clip Event
to a Movie Clip
Attach an onClipEvent
to a Movie Clip
Create or open a Flash document
(ActionScript 2.0), place a movie
clip on the Stage, and then select
the movie clip
Give the movie clip a unique
instance name in the Properties
panel
Move down the Timeline and add a
keyframe at frame 80
Click the Insert menu, point to
Timeline, and then click Keyframe.
Add a second movie clip to the
Stage, and then select the second
movie clip
Enter the script as shown in the
illustration
Click the Control menu, point to
Test Movie, and then click Test.
When the playhead hits frame 80 it
loads the second movie clip The
loading of the movie will trigger
the onClipEvent handler, and stop
the playing of the movie clip with
the unique instance name of
movie2
7
6
5
4
3
2
1
3 5
6 1