Say you create a button, and you want the ActionScript to trigger an instruction that will stop the movie, but only when the user clicks the button object.. What You’ll Do View the Actio
Trang 114
14
Using Basic ActionScripts
Introduction
Flash's programming language is called ActionScript
ActionScript lets you create detailed instructions on how the
Flash movie will perform Flash CS5 allows you to build
applications using two different versions of ActionScript,
either ActionScript 1.0 & 2.0 or ActionScript 3.0
In this chapter, you’ll learn the basics of ActionScript
along with scripting as it relates to ActionScript 2.0 In the
next chapter, you’ll learn about scripting as it relates to
ActionScript 3.0 ActionScripts can use an event to trigger the
specific action Say you create a button, and you want the
ActionScript to trigger an instruction that will stop the movie,
but only when the user clicks the button object The defined
event is the user clicking his mouse, and the action would be
to stop playing the movie ActionScript is a relatively easy
language to learn, but a very precise language to code, so
pay close attention to the syntax
The good news is that once you master the language and
the syntax, the full power of Flash is available to you You can
create ActionScripts that are triggered (the event) by specific
data, or information typed in by a visitor You can even create
ActionScripts that are sensitive to variables such as date and
time Flash helps you by giving you functions (English-like
script) and as your ActionScript skills grow, you can even
cre-ate and call your own functions Each new version of Flash
moves the ActionScripting language closer and closer to
JavaScript The power of Flash is fully realized when you
write ActionScripts and incorporate them in your Flash
docu-ments
What You’ll Do
View the Actions Panel Set ActionScript Preferences Understand Properties and Methods Apply Properties and Methods to
an Object Use Dot Syntax Understand Data Types Use Functions
Use Conditional Statements Attach a Mouse Event to a Button Work with Frame Events
Work with Clip Events Attach a Clip Event to a Movie Clip Work with Loops
Use For Loops Work with ActionScript Behaviors
Trang 2348 Chapter 14
The Actions panel is gives the Flash designer
control of a Flash document by allowing
him/her to create and edit actions for an
object or frame To use the Actions panel,
select an object on the stage, or select a
frame on the Timeline, click the Window
menu, and then click Actions Scripts can be
typed directly into the Actions panel using the
Script pane, or augmented by using a list of
installed Actions in the Toolbox
◆ Toolbox Supplies a list of all installed
actions, either ActionScript 1.0 & 2.0 or
ActionScript 3.0
◆ Script pane Enter the Actions into the
Script pane
◆ Script Navigator pane Gives reference
to all the Scripts in the active movie
◆ Current Script tag Indicates which
script is being edited
◆ Pin Script Adds a tab for a selected
script
◆ Options menu Contains options that
control and format the Actions panel
◆ Add Statement Lets you add script
elements to the current action
◆ Find, and Find and Replace Searches
the active script
◆ Insert Target Path Inserts a specific target clip into the action
◆ Check Syntax Checks the current action for syntax errors
◆ Auto Format Cleans up the script by auto indenting
◆ Auto Close Bracket When you type
an open bracket {, Flash automatically adds the corresponding close
bracket } (New !)
◆ Show Code Hint Gives you hints to the syntax of the action as you type
breakpoints into the action to pause
on the specified line of code
◆ Collapse and Expand Collapse between braces, collapse selection,
or expand all
◆ Comments Apply a block or line comment
◆ Code Snippet Code Snippets provides ActionScript 3.0 code segments to use in scripts (New !)
◆ Script Assist for 3.0 Script assist provides a visual interface for editing scripts (syntax completion and parameter descriptions)
◆ Help Provides online help
Viewing the Actions Panel
Script Tag
Script Navigator pane
Toolbox
Script pane
Help Script Assist Options menu
Pin Script Actions Panel toolbar buttons Code Snippets
Trang 3Since ActionScripting is so important, Flash gives you the ability to con-trol the Actions panel through preferences ActionScript preferences give you the ability to control the font and size of the text typed into the Actions panel, as well as using syntax coloring to help you visualize the code You can also set AutoFormat preferences to specify the auto-matic formatting you want for your ActionScript code
Setting ActionScript
Preferences
Set ActionScript Preferences
Click the Flash (Mac) or Edit (Win)
menu, and then click Preferences.
Click the ActionScript category.
Select from the following options:
◆ Automatic Close Brace Select
to automatically insert a close
bracket (New !)
◆ Automatic Indentation.
Instructs Flash to perform
syntax indentation
◆ Tab Size Enter a value for the
number of spaces used
◆ Code Hints Gives you
on-screen hints as you type
◆ Delay Delay before showing a
code hint (0 to 4 seconds)
◆ Font Select a font and size for
the ActionScript text
◆ Open/Import and Save/Export
Select UTF-8 or Default
encoding for open and import
operations (UTF-8 is best)
◆ Reload Modified Files Click to
be prompted when Flash needs
to reload a modified file
◆ Class Editor Select an editor or
Ask to be prompted (New !)
◆ Syntax Coloring Choose the
syntax-coloring scheme
◆ Language Click ActionScript 2.0
or ActionScript 3.0 to modify the
ActionScript sub-settings
Click the Auto Format category.
Select format check boxes and
view the effect in preview
Click OK.
6
5
4
3
2
1
3 2
6
5 4
To reset preference setting to the default, click Reset To Defaults.
Preview area
Trang 4350 Chapter 14
Objects in Flash are defined using two primary identifiers: properties and methods The
proper-ties of an object define its characteristics In the real world, a house would be an object, and its
properties would be things like its color, style, and number of windows and doors In Flash, it
would be written something like this:
house.color = "green";
house.style = "ranch";
house.windows = "12";
house.doors = "2";
In this example, the word house is a unique instance name for the house object, and the words
color, style, windows, and doors represent the properties assigned to this instance Think of an
instance as a copy of a Library item When you create a movie clip, the object is created in
Flash's Library When you return to the Stage, you can then drag the movie clip from the
Library to the Stage (technically, you're moving a Library symbol, created using the movie clip
behavior) Once the movie clip is moved to the Stage, it is defined as an instance of the original
Library item When you select an instance on the Stage, Flash's Properties panel lets you give it
a unique name In the previous example, "house" is the unique name
Giving a Library symbol a unique name gives you a lot of control For example, you could
move two instances of the same movie clip onto the Stage, give each of them its own unique
name (house1, house2) in the Properties panel, and then define different properties for each
one Something like this:
house1.color = "green"; house2.color = "blue";
house1.style = "ranch"; house2.style = "tudor";
house1.windows = "12"; house2.windows = "8";
house1.doors = "2"; house2.doors = "4";
In Flash, most objects have properties For example, the MovieClip object has property
val-ues such as transparency, horizontal and vertical position, and visibility You might define
prop-erties loosely as the physical appearance of a movie clip, as it appears on the Flash Stage A
method instructs the object to perform some task A method is a function that is part of a class
definition For example, if a DVD player is an object, then the methods would be something
like: play, record, and stop Flash methods are written like this:
play();
record();
stop();
Some methods require parameters within the parenthesis The following method instructs the
play head to move to frame 6 on the Timeline and stop: gotoAndStop(6);
Attaching the method to a specific object requires identifying the object in the ActionScript
code: myDVD.gotoAndStop(6);
ActionScript is a language, and just like learning any foreign language, all the words and
syn-tax might seem strange at first; however, the longer you work with the language, the simpler it
becomes
Understanding Properties and Methods
Trang 5Once you've gotten the hang of writing ActionScripts, the next step is
to apply properties and methods to objects in a Flash document You can have an object and let your visitor control its color Changing the color is an example of changing an object’s properties To make an object change color, you will need a Flash document that contains a MovieClip and button symbols An easy script translation would be:
Flash, when my visitor clicks (release) on the button, I want you to assign a new color to an object that I gave a unique instance name (change it), and I'm defining that property as objectColor, and change the color (setRGB) to red (0x990000) When you attach ActionScripts to buttons, you're not limited to just a single use For example, you could drag three instances of the same button symbol on the Stage and repeat the previous code with one exception: change the SetRGB value
of one to (0x990000) for red, another to (0x009900) for green, and the third one to (0x000099) for blue
Applying Properties
and Methods to an
Object
Apply Properties and Methods
Drag the movie clip onto the
Stage
Enter a unique instance name in
the Properties panel
Drag the button symbol onto the
Stage, and then select the symbol
Click the Window menu, and then
click Actions to open the Actions
panel
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
Click the Control menu, point to
Test Movie, and then click Test.
Click the button to change the
color of the object to red
7
6
5
4
3
2
1
2
1 3
5
See Also
See “Creating Invisible Buttons” on
page 153 for information on creating
invisible buttons