ptg
352 Chapter 14
ActionScript, just like any human language,
has rules you should follow. However, in
ActionScripts, you have to follow the rules or
it won't work. One of the more important
rules to follow is the use of dot syntax. The
use of dots (.) in a script serves several pur-
poses. First, is to specify the target path to a
particular Timeline. For example, _root.amer-
ica.kansas.wichita defines a movie clip on the
main (_root) Timeline with the name america,
containing a movie clip named kansas, which
in turn contains a movie clip named wichita.
Dot syntax is used to create a road map for
Flash to follow. Dot syntax is a separator
between two or more parts of a Flash script.
Another use of dot syntax is to change the
properties and methods for a specific object.
Since ActionScript, by definition is an object-
oriented language, Flash performs its respon-
sibilities by instructing an object to do
something (method), or by changing a prop-
erty. For example, star._rotation = 90; instructs
Flash to rotate the MovieClip instance named
star
, 90 degrees (property). To instruct the star
MovieClip instance to play (method), you
would enter the following code: star.play();
Using Dot Syntax
Movie Clip with Instance Name:
MovieA
ActionScript 2.0
From the Library of Wow! eBook
ptg
Chapter 14 Using Basic ActionScripts 353
When you use ActionScripts, you have the
ability to work with data. Data can be informa-
tion entered in by a visitor in an input data
field, or it can be computer information such
as the current position of the mouse, or the
date and time. When you work with data, you
have 4 possible data types:
◆
String.
Allows for the entering of text
values.
◆
Number.
Allows for the entering of
numeric values.
◆
Boolean.
A Boolean state has two
values; typically true or false.
◆
Object.
Serves as a storage device for
any data type, including other objects.
Use Data Types to Control Information
Since data types control the type of informa-
tion that can be entered into a data field, you
can use them to validate the data someone is
entering. For example, you want to create a
calculator to convert between degrees
Fahrenheit and Celsius. To do this, you will
need an input field for someone to enter the
current temperature in Fahrenheit, a button
that would perform the calculation, and then a
dynamic text field for the result, and one for
an error message.
Select the Input Field, and then give it a
unique variable name in the Properties
panel.
Select the Error Dynamic Text Field and
give it a unique variable name in the
Properties panel.
Select the Results Dynamic Text Field,
and give it a unique variable name in the
Properties panel.
Select the button instance, and then
enter a script into the Actions panel (see
the example file for script details).
When the movie is played, the visitor will
enter a value into the data field, and it will be
evaluated as to whether it's purely numeric. If
it isn't, an error message will display in the
dynamic error field. If the field contains num-
bers, then the calculation will perform the
conversion and display the results displayed
in the output field.
4
3
2
1
Understanding Data Types
Error Message using text
Error Message using a
blank field
Correct data no error
message
From the Library of Wow! eBook
ptg
354 Chapter 14
A
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 u s e 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.
From the Library of Wow! eBook
ptg
Chapter 14 Using Basic ActionScripts 355
Conditional 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
From the Library of Wow! eBook
ptg
356 Chapter 14
Attaching 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
From the Library of Wow! eBook
ptg
Chapter 14 Using Basic ActionScripts 357
Frame 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
From the Library of Wow! eBook
. syntax is used to create a road map for
Flash to follow. Dot syntax is a separator
between two or more parts of a Flash script.
Another use of dot syntax. 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