ActionScript Lecture Notes -
Objective #1: Use ActionScript to use actions to control a movie.
- You can work in the normal mode or the expert mode in the Script pane. The
normal mode prompts you with choices as build an ActionScript statement. However,
it can be awkward for those with average ActionScript experience. The expert
mode requires you to type out most of your ActionScript code from scratch,
however you can see a whole section of statements at once.
- When using commands such as gotoAndPlay it is best to use frame labels rather
than frame numbers as parameters. By entering frame labels in frames throughout
your movie's timeline, you can organize your movie better especially if its
meant to be used as a web site with different static pages. I recommend creating
a layer called "labels" and entering the frame labels in keyframes
in that layer. To add a frame label, add a keyframe and with the frame selected,
enter a label in the Property Inspector.
- The syntax rules for typing ActionScript are important to follow since it
is a programming language. Do your best to type actions such as gotoAndPlay with the proper mix of upper and lowercase letters. Also, do your best to
add semicolons where necessary.
- A comment statement is a statement that is typed into the code for your
reference but ignored by Flash. In the following example the first statement
is a comment statement,
// Home button
on (release) {
gotoAndPlay("home");
}
- Comment statements can also be typed by beginning a section of code with
a /* and ending the commented section with */. See this example,
/*
Home button
*/
on (release) {
gotoAndPlay("home");
}
- With statements like the on statement that is used with
buttons, Flash waits until the mouse button is released. When the button is
released, Flash executes the statement or statements that are in the curly
braces { .... }
Therefore you must type the gotoAndPlay action statement inside of the curly
braces. Indentation and line breaks aren't important to Flash but you should
follow conventional style. The following versions of code will work equivalently
although the first version is the most conventional and the easiest to read:
on (release) {
gotoAndPlay("Page2");
}
on (release)
{
gotoAndPlay("Page2");
}
on (release) { gotoAndPlay("Page2"); }
on (release)
{gotoAndPlay("Page2")}
Note that the semicolon was not typed in the last version but the code will
still work only because Flash is very forgiving....in the current MX version
at least.
Objective #2: Use targets and movie clip symbols.
- You should use movie clips symbols since it is good to use symbols for the
sake of size optimization and efficient design but also because more can be
done with movie clip symbols than with graphic symbols.
- Since a movie clip contains its own independent timeline, it is sometimes
desirable to use ActionScript to control that timeline. To refer to a movie
clip and/or its timeline, you must first make it a target by giving it a unique
name.
- When you select a movie clip symbol that is on the Stage, you can type a
target name in a field in the upper-corner of the Property Inspector. This
gives the instance of the movie clip symbol a name which is unrelated to the
name of the movie clip symbol itself that appears in the Library.
- Once you have named a movie clip instance, it can be referred to in a with
statement like this
on (release) {
with (walkingMan) {
gotoAndPlay("start");
}
}
By placing the with statement inside of the curly braces to the on statement
and then placing a gotoAndPlay statement inside the curly braces of the with
statement, you are telling Flash that when the mouse button is released it
should play the frame labelled "start" that is on the timeline of
the walkingMan movie clip instance. If you incorrectly typed the following
code that does not include the with statement
on (release ) {
gotoAndPlay("start");
}
then Flash would look for a frame labelled "start" on the movie's
main timeline.
- Occasionally, you'll find the need to use a movie clip within a movie clip.
For example, if a movie clip symbol instance with the name walkingMan has
a nested a movie clip instance with the target name movingLegs within it,
you can play a specific frame of the innermost movie clip from a button on
your main movie's timeline. In this case, you should use the Insert Target
Path dialog box to be sure to use the correct syntax in the with statement.
Or, you could understand the necessary dot notation that is required in this
case and type out the code yourself.
on (release) {
with (walkingMan.movingLegs) {
gotoAndPlay("rightlegforward");
}
}
In the expression walkingMan.movingLegs, the dot symbol is used to show the
hierarchical, parent-child relationship between the two instances. In this
example, a frame labelled "rightlegforward" must exist in the movingLegs
instance. In this example, you must remember to specify the nested instance
with the target name movingLegs.
Objective #3: Create interactive movie clip symbols.
- A number of actions including startDrag, stopDrag, _droptarget can be used
with movie clips to make for a more interactive user experience.
- A startDrag action can be typed inside an on statement to allow the user
to drag a movie clip across the stage. Select a movie clip and then type the
following code into the Action pane
on (press) {
startDrag("");
}
to allow the user to drag a movie clip instance across the stage. You can
also type the keyword this inside the parentheses of the startDrag action.
You should follow up the code above with the code
on (release) {
stopDrag( );
}
to allow the user to drop the movie clip that he/she is dragging.
Furthermore, you can use the _droptarget and setProperty actions to make a
movie clip instance disappear when the user drops it over top of another named
movie clip instance. In the example below
on (press) {
startDrag("");
}
on (release) {
stopDrag( );
if (_droptarget=="/targetInstance") {
setProperty("_root.draggedInstance",
_visible, false);
}
}
the movie clip instance with the name draggedInstance can be dragged across
the screen if this code is typed into its Action pane. However, when it is
overtop of the movie clip instance with the name targetInstance, it will suddenly
disappear. The _droptarget function requires the forward slash in front of
the instance name in the double quotes. Note that _root is typed out to provide
the full absolute path of the draggedInstance target. You can tell that _visible is a property of a movie clip since it begins with an underscore.
- You can use ActionScript to change properties of a movie clip instance such
as position, rotation, color, size, and visibility.
Objective #4: Define and use variables in ActionScript statements.
- A variable is used in a programming language to store a numeric or text
value for the purpose of using that value later in the program. A variable
can be used to store the current score to a game or a user's choice from a
menu. When you create a variable you must give it a name that contains no
spaces such as score or userScore and you set it equal
to a given value with an equals sign. The following statement sets the variable
score equal to zero.
score = 0;
You might want to place a statement like that in the Action pane with frame
1 selected. You can also perform arithmetic on variables and store the result
in another variable. Consider the following example
totalScore = partOneScore + partTwoScore;
in which the variable totalScore is now equal to the sum of partOneScore plus
partTwoScore after the statement executes. Do not type such statements with
the variable that you want the new value to be stored in on the right side
of the equals symbol since
partOneScore + partTwoScore = totalScore;
would cause a syntax error.
- Mathematical operators *, /, +, -, and % (modulus) can be used with variables
in assignment statements like the examples above. Be careful when using the
addition + operator though. Flash will sometimes concatenate two numbers together
by default. That is the variable sum will store the string value "32"
after the following statement executes
sum = 3 + 2;
so you may need to use the Number function to force Flash to treat a value
as a number
sum = Number(3) + Number(2);
- Variables can also store strings (i.e. words or phrases) which must be
surrounded by double quotes. Example,
name = "Santa Claus";
- An if statement can be used to determine whether a certain condition is
true or false and, if it's true, you can specify that a given action take
place. Even though we haven't studied variables yet in the following example
assume that there is a variable named score that stores a whole number such
as 3
on (release) {
if (score = = 3) {
gotoAndPlay(25);
}
}
When the user releases the mouse button the code inside of the curly braces
to the on statement will execute. Then, if and only if the number currently
stored in the variable score is 3, Flash will play frame number 25. If the
variable score is not equal to 3 then nothing special happens and the movie
plays whatever frame it normally would have played. Inside the parentheses
of an if action is a condition that is tested to either be true or false.
A double equals symbol must be used when you want to test whether a variable's
value is equal to a certain number or not. Other operators that can be used
in place of the double equals symbol are < , >, <= , >=, and !=
(not equal to).
- You can also use an if else statement or an if elseif conditional statement.
For example,
on (release) {
if (score > 10) {
gotoAndPlay("gameOverWinner");
}
else {
gotoAndPlay("gameOverLoser");
}
}
on (release) {
if (score >10) {
gotoAndPlay("congratulations");
}
elseif (score > 5) {
gotoAndPlay("okay");
}
elseif (score > 0) {
gotoAndPlay("belowAverage");
}
}
- Three kinds of loops, while, do/while and for, can be used to make a set
of statements execute over and over again.
- The + operator is the concatenation operator in Flash and can be used to
join two strings together into one string as in
wholeName = firstName + " " + lastName;
Objective #5: Use input and dynamic text fields to add interactivity
to a Flash movie.
- When you use the Text tool to create text, you are creating static text
by default. With a text field selected, you can change it to input text or
dynamic text in the Property Inspector. An input text field allows the user
to type text into the text field when the movie file executes. A dynamic text
field is used instead of static text when you want to change the text that
is entered there using ActionScript when the movie executes. Either way, be
sure to specify a unique name (that includes no spaces) in the Var area of
the Property Inspector for an input or dynamic text field. This variable name
can then be used in your ActionScript code to refer to the text that the user
typed into an input text field or to change the text that appears in a dynamic
text field. For example, if a dynamic text field had the name gameScore
typed into its Var field, the following statement would make the value 100
appear in the text field when the statement executes
gameScore = 100;