Professional Documents
Culture Documents
Flash ActionScript
versions ™
techniques, including: • Photoshop tryout from Adobe
Flash ActionScript
• Paint Shop Pro evaluation
• Assigning ActionScript to a frame • Setting volume and panning version from JASC Software
• Testing a movie • Creating a scrollable text box • Plus a searchable e-version of
• Concatenating strings • Working with multiple movies the book
™
“Apply It” and “Extra” High-resolution screen Succinct explanations Two-page lessons
sidebars highlight shots demonstrate walk you through break big topics into Etheridge
useful tips each task step by step bite-sized modules
Your visual blueprint for creating
ISBN 0-7645-3657-5 Flash-enhanced Web sites
*85 5 -BADB f $26.99 USA
$39.99 CAN
£21.99 UK incl.VAT
Category:
Web Development
www.hungryminds.com
System requirements: PC running Windows 98 or later. See “What’s on the CD-ROM” appendix for details
and complete system requirements.
,!7IA7G4-fdgfhb!:p;m;Q;t;T Web development tools on CD-ROM!
• Macromedia Flash, FreeHand, and Flash Player trial versions
• Plus sample graphics, a searchable e-version of the book, and more
Visit us at www.
hungryminds.com
013657-5 FM.F 1/31/02 12:15 PM Page i
Flash ActionScript
Your visual blueprint for
creating Flash-enhanced Web sites
by Denise Etheridge
TM
From
®
&
Best-Selling Books • Digital Downloads • e-Books • Answer Networks • e-Newsletters • Branded Web Sites • e-Learning
New York, NY • Cleveland, OH • Indianapolis, IN
013657-5 FM.F 1/31/02 12:15 PM Page ii
Flash ActionScript
Your visual blueprint for
creating Flash-enhanced Web sites
013657-5 FM.F 1/31/02 12:15 PM Page iv
At maranGraphics, we believe in producing great rebuild every screen shot to provide the best clarity
computer books — one book at a time. possible, making our screen shots the most precise and
easiest to read in the industry. We strive for perfection,
maranGraphics has been producing high-technology and believe that the time spent handcrafting each
products for over 25 years, which enables us to offer the element results in the best computer books money
computer book community a unique communication can buy.
process.
Thank you for purchasing this book. We hope you
Our computer books use an integrated communication enjoy it!
process, which is very different from the approach used
in other computer books. Each spread is, in essence, a Sincerely,
flow chart — the text and screen shots are totally
incorporated into the layout of the spread. Introductory Robert Maran
text and helpful tips complete the learning experience. President
maranGraphics’ approach encourages the left and right maranGraphics
sides of the brain to work together — resulting in faster
orientation and greater memory retention. Rob@maran.com
Above all, we are very proud of the handcrafted nature www.maran.com
of our books. Our carefully-chosen writers are experts
in their fields, and spend countless hours researching www.hungryminds.com/visual
and organizing the content for each topic. Our artists
013657-5 FM.F 1/31/02 12:15 PM Page v
CREDITS
ACKNOWLEDGMENTS
Hungry Minds Technology Publishing Group: Bob Ipsen, Vice President & Publisher; Richard Swadley, Vice
President & Executive Group Publisher; Mary Bednarek, Vice President & Publisher, Networking; Joseph
Wikert, Vice President & Publisher, Web Development Group; Mary C. Corder, Editorial Director, Dummies
Technology; Andy Cummings, Editorial Director, Dummies Technology; Barry Pruett, Vice President &
Publisher, Visual/Graphic Design
Hungry Minds Marketing: John Helmus, Assistant Vice President, Director of Marketing
Hungry Minds Production for Branded Press: Debbie Stailey, Production Director
Hungry Minds Sales: Michael Violano, Vice President, International Sales and Sub Rights
013657-5 FM.F 1/31/02 12:15 PM Page vi
AUTHOR’S ACKNOWLEDGMENTS
I would like to say thank you to all of people who provided me with assistance or
support during the development of this book. Thank you to Neil Salkind and
Jennifer Dorsey for providing me with the opportunity and for encouraging me
along the way. Thank you to Jade Williams for her editing and project
management. And, thank you to all of the editors and staff at John Wiley who
assisted Jade.
I would like to give a special thanks to David Gregory for assisting with the
artwork and Malinda McCain for helping me with this and many other projects.
013657-5 FM.F 1/31/02 12:15 PM Page vii
TABLE OF CONTENTS
viii
013657-5 FM.F 1/31/02 12:15 PM Page ix
FLASH ACTIONSCRIPT:
Your visual blueprint for
creating Flash-enhanced Web sites
ix
013657-5 FM.F 1/31/02 12:15 PM Page x
TABLE OF CONTENTS
x
013657-5 FM.F 1/31/02 12:15 PM Page xi
FLASH ACTIONSCRIPT:
Your visual blueprint for
creating Flash-enhanced Web sites
9) DEMYSTIFYING FUNCTIONS
Get Properties ..................................................................................................................210
Using Get Timer and Get Version ..................................................................................212
Using Eval ..........................................................................................................................213
Convert a String to a Number ........................................................................................214
Evaluate for Mathematical Errors ..................................................................................216
Create a Scrollable Text Box ..........................................................................................218
Create a Custom Function ..............................................................................................220
xi
013657-5 FM.F 1/31/02 12:15 PM Page xii
TABLE OF CONTENTS
xii
013657-5 FM.F 1/31/02 12:15 PM Page xiii
FLASH ACTIONSCRIPT:
Your visual blueprint for
creating Flash-enhanced Web sites
APPENDIX
What’s on the CD-ROM ..................................................................................................294
Using the E-Version of the Book ....................................................................................296
Hungry Minds, Inc. End-User License Agreement ........................................................298
INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .300
xiii
013657-5 FM.F 1/31/02 12:15 PM Page xiv
xiv
013657-5 FM.F 1/31/02 12:15 PM Page xv
Flash ActionScript:
Your visual blueprint for
creating Flash-enhanced Web sites
to collect information from the user, record what a user movie clip within a Flash movie. In fact, multiple instances
has done, change values as a movie plays, or evaluate of a movie clip symbol can be embedded inside of each
whether a condition is true or false. A string is any other. This chapter steps you through sample scripts that
sequence of characters consisting of any combination of work with multiple Timelines.
letters, numbers, or punctuation marks. The ActionScript Chapter 11, Creating Smart Clips, teaches you how to
String object has several methods that enable you to create smart clips. A smart clip is a movie clip with
manipulate strings. This chapter explains variables and ActionScript that can be reprogrammed without using the
strings and steps you through sample scripts that use Actions panel. This enables you to create ActionScripts that
variables and strings. people with no programming ability can modify.
Chapter 5, Working with Operators, explains operators and Chapter 12, Using Creative Techniques, pulls all basics of
steps you through sample scripts that use operators. An ActionScript together by combining actions and
operator is a character used to specify how to combine, demonstrating techniques that you can use when creating
compare, or modify a value in an expression. For example, a Flash movie.
the plus (+) operator can be used to add two numbers; the
Minus (–) operator can be used to subtract two numbers. Chapter 13, Debugging ActionScript, is on debugging
ActionScript. Flash movies that contain ActionScript can
Chapter 6, Changing the Script Flow, steps you through become complex. Flash provides you with several tools
actions that enable you to change the flow of script. In that can help you troubleshoot your movie.
ActionScript, each statement is executed in order from top
to bottom unless you change the flow of a script. The final chapter, Flash Action Script Quick Reference,
contains useful tables of reference material and a summary
Chapter 7, Using the Movie Clip and Array Objects, of commands.
introduces the MovieClip object, which is the most
important object in ActionScript. Since each movie clip
has its own Timeline, each movie clip can be a complete What Is On The CD-ROM
animation. Arrays enable you to group values together and The CD-ROM in the back of this book contains the sample
are useful when you need to store and retrieve lists of code from each of the two-page lessons, as well as the
data. The ActionScript Array object has several methods code from most of the Apply It sections. This saves you
that enable you to manipulate arrays. from having to type the code and helps you quickly get
Chapter 8, Working with Objects, steps you through started creating Flash ActionScript programs. The CD-ROM
ActionScript examples that use objects contains several also contains several shareware and evaluation versions of
objects that you can use to manipulate symbols and data. programs that can help you work with Flash ActionScript.
For example, the Math object is used to manipulate An e-version of this book is also available on the
numbers. You can use the Math object’s round method to companion disc.
round a number. The Color object is used to manipulate Chapter 1 provides a very brief overview of how to create
color. You can use the methods of the Color object to text, input text boxes, dynamic text boxes, buttons, and movie
change the color of a movie clip. clips. Since this book assumes you know Flash, all of the text,
Chapter 9, Demystifying Functions, teaches you how to input text boxes, dynamic text boxes, buttons and movie clips
create you own custom functions. Functions are reuseable used in the sample files have been created for you.
blocks of ActionScript. You can pass values to a function or The sample files all have an FLA extension. Copy the files
you can use a function to obtain values. For example, you to the directory of your choice and click Files ➪ Open on
use the getVersion function to get the version of Flash the Flash menu to open them.
Player. This chapter explains functions and steps you
through sample scripts that use functions.
Chapter 10, Working with Multiple Timelines, explains how
to work with multiple Timelines in a single movie. You can
lay out the sequence of a Flash movie on a Timeline. Movie
clips are mini Flash movies. They each have their own
Timeline and their own properties. You can embed a
xv
400 Part IX: Appendixes
3657-5 Ch01.F 1/31/02 12:16 PM Page 2
FLASH ACTIONSCRIPT
INTRODUCTION TO FLASH
AND ACTIONSCRIPT
lash is a graphics and animation software package that ActionScript, the scripting language that enables you to add
FLASH INTERFACE
2
3657-5 Ch01.F 1/31/02 12:16 PM Page 3
CREATE A SCENE
sing scenes enables you to separate your movie into To create a scene, click View ➪ Scene from the menu. A
3
3657-5 Ch01.F 1/31/02 12:16 PM Page 4
FLASH ACTIONSCRIPT
CREATE LAYERS
hen creating a Flash movie, you place individual on a higher layer. You can change the order of layers and the
CREATE LAYERS
Layer
Layer 3
⁄ Click Insert ➪ Layer to ■ A new layer appears above › Click in the Lock/Unlock
create a layer above the the active layer. All Layers column to toggle
active layer. between locking ( ) and
¤ Double-click and type to unlocking ( ) layers.
change the layer name.
ˇ Click in the Show Layers
‹ Click in the Show/Hide All as Outlines column to toggle
Layers column to toggle between Show Normal ( )
between showing ( ) and and Show As Outline ( ).
4 ( ) hiding a layer.
3657-5 Ch01.F 1/31/02 12:16 PM Page 5
When you draw on an object using the Line, You can use the Layer Properties dialog box to change
Oval, Rectangle, Pencil, or Brush tool, the object layer properties. To open the Layers Properties dialog
segments. Each segment is an individual object box, choose Modify ➪ Layer from the menu. Use the
that you can select, move, and reshape. When Name field to name the layer. Select Show to show
you cover object A with object B, object B the layer. Select Lock to lock the layer. Select the Type
connects to, segments, and erases object A. Place of layer. Choose from Normal, Guide, Guided, Mask,
objects on separate layers to prevent them from and Masked. Use the Outline Color option to select
connecting, segmenting, and erasing. the color of the outline. To view a layer as an outline,
select View Layer As Outlines. Select a Layer Height.
Choose from 100 percent, 200 percent, or 300
percent.
Layer 3 Layer 3
Á Click the layer and drag to ° Click to add a guide ‚ Click the Show/Hide, ■ Items on lower layers
change the order of a layer. layer. Lock/Unlock, or Outlines appear to be behind items
icon to affect every layer in on higher layers.
‡ Click to insert a layer. · Click to delete a layer. the column.
■ The pencil icon indicates
this is the active layer.
5
3657-5 Ch01.F 1/31/02 12:16 PM Page 6
FLASH ACTIONSCRIPT
CREATE GRAPHICS
lash provides you with several tools you can use to selected objects. Grouping enables you to manipulate
CREATE GRAPHICS
⁄ Click to select a tool. ¤ Click to select modifiers. ‹ Move to the Stage and ■ You can create a square
drag to create a shape. with or a circle with by
holding down the Shift key as
■ You can create rectangles you drag.
with the Rectangle tool ( ),
ellipses with the Oval tool
( ), straight lines with the
Line tool ( ), paint with the
Brush tool ( ), or free-form
lines with the Pencil tool
( ), or you can erase with
6 the Eraser tool ( ).
3657-5 Ch01.F 1/31/02 12:16 PM Page 7
To hide the Toolbox, click Window ➪ Tools from You can click Windows ➪ Panels ➪ Fill to open the
the menu. To bring the Toolbox back, click Fill panel. The Fill panel enables you to select color,
Window ➪ Tools again. gradients, or bitmap fills. You can create your own
gradients. You can click any bitmap in the Library as
Use the Hand tool to move the Stage. Use the
a bitmap fill. You can click Windows ➪ Panels ➪
Zoom tool to zoom in on the Stage. You can also
Stroke to select a stroke color, style, or line weight.
click View ➪ Zoom In to zoom in on the Stage,
Clicking Windows ➪ Panels ➪ Mixer enables you to
or View ➪ Zoom Out to zoom out. To change
create colors.
the magnification, click View ➪ Magnification
and select a magnification level.
■ If you selected the Ink ■ You can click the stroke › Click to select the Lasso or ˇ Move to the Stage and
Bottle ( ), Paint Bucket line to change the stroke Arrow tool. drag the mouse to select an
( ), or Dropper tool ( ), color with , the fill color to object.
move to the Stage and click change the fill with , or set ■ The pointer changes to the
the mouse. a stroke or fill color with . icon that represents the tool ■ Use to place a rectangle
you selected. around the selected area.
■ Use to draw the area
you want to select.
7
3657-5 Ch01.F 1/31/02 12:16 PM Page 8
FLASH ACTIONSCRIPT
CREATE A SYMBOL
reating and using symbols enables you to share to create an animated button. You use ActionScript to give
■ Make sure nothing on the ⁄ Click Insert ➪ New ■ The Symbol Properties › Click OK.
Stage is selected. Symbol to open the Symbol dialog box appears.
Properties dialog box. ■ Flash changes to the
¤ Type a symbol name. symbol-editing mode.
‹ Click to select a behavior.
8
3657-5 Ch01.F 1/31/02 12:16 PM Page 9
You can convert any selection on the Stage into a When you create a symbol from a selection on the
symbol. To reduce the file size of your movies, Stage, the selection becomes an instance of the
consider turning background images into symbol. You will no longer be able to edit the
symbols so you can reuse them. To convert an object directly. To edit the symbol, you must move
object on the Stage into a symbol, use the Arrow to the symbol-editing mode. To move to the
tool to select the object. Click Insert ➪ Convert symbol-editing mode, use the Arrow tool to select
to Symbol to open the Symbol Properties dialog the symbol and click Edit ➪ Edit Symbols. When
box. Click to select Movie Clip, Graphic, or you have completed editing your symbol, click
Button as the behavior. Click OK. Edit ➪ Edit Movie to exit the symbol-editing mode.
When you modify a symbol, Flash updates every
instance of the symbol in the movie. When editing a
symbol, you can use all of the drawing tools and
you can import content.
Circle
ˇ Click to select a drawing ‡ Click the Scene button to ■ The main Timeline returns. · Click the symbol name to
tool and create the symbol exit symbol-editing mode. see the symbol in the Item
content. ° Click the Show Library Preview window.
icon ( ) to open the Library.
Á Create a symbol. ■ The symbol you created
appears in the preview
window.
9
3657-5 Ch01.F 1/31/02 12:16 PM Page 10
FLASH ACTIONSCRIPT
CREATE AN INSTANCE
reating and using instances of a symbol can completely opaque. You use the Effect panel to change the
CREATE AN INSTANCE
Library Ctrl+L
10
3657-5 Ch01.F 1/31/02 12:16 PM Page 11
You use the Effect panel to change the brightness, You can use the Instance panel to change the
tint, or alpha property of an instance. To open behavior type of an instance. To open the Instance
the Effect panel, click Window ➪ Panels ➪ Effect. panel, click Window ➪ Panels ➪ Instance or click
Select Brightness and use the brightness slider to Modify ➪ Instance on the menu. Use the Arrow
set the brightness to a value between -100 and Tool to select the instance for which you want to
100. Select tint to change color using a color change the behavior. Use the Behavior field to
box or by setting Red, Green, and Blue values. change the behavior of your instance.
Select Alpha and use the Alpha slider to set the
brightness to a value between 0 and 100 percent.
Select Advanced to adjust both the color and
alpha values.
Circle Circle
■ The Library window ‹ Click the symbol name. › Drag the symbol from the ■ An instance of the symbol
appears. Library onto the Stage. appears on the Stage.
11
3657-5 Ch01.F 1/31/02 12:16 PM Page 12
FLASH ACTIONSCRIPT
CREATE A BUTTON
uttons enable you to add interactivity to your Flash When you create a button, Flash presents you with a four-
CREATE A BUTTON
12
3657-5 Ch01.F 1/31/02 12:16 PM Page 13
Enabling a button lets you test the button. Flash You can import the image you use to define a
disables buttons by default, making it easier for button state. To import an image click File ➪
you to work with them while you create your Import, locate the image you want to import,
movie. To enable a button for testing, click and click Open.
Control ➪ Enable Simple Buttons. A check mark
appears next to the selection on the menu.
When you move your pointer over the button,
the Over state appears. When you click while
the pointer is over the button, the Down state
appears. To select an enabled button, use the
Arrow tool to draw a rectangle around the
button. To move an enabled button, select it with
the Arrow tool and use the arrow keys to move
the button around the Stage. To disable a button
after enabling it, click Control ➪ Enable Simple
Button again. When you are creating a movie, it
is best to leave your buttons disabled and only
enable your buttons for testing.
Keyframe F8
ˇ Define the appearance of ■ Flash inserts a keyframe ■ The main Timeline returns. · Drag an instance of the
the Up state by drawing or and copies the Up state button from the Library onto
placing an instance on the image to the Over state ° Click the Show Library the Stage.
Stage. frame. You can change the icon ( ) to open the Library.
image if you want. ■ An instance of the button
Á Click Insert ➪ Keyframe. appears on the Stage.
■ Repeat steps 5 and 6 for
the Down and Hit states.
‡ Click the Scene button to
exit the symbol-editing mode. 13
3657-5 Ch01.F 1/31/02 12:16 PM Page 14
FLASH ACTIONSCRIPT
CREATE ANIMATION
ou use Flash to create animation. You can rotate or The two types of tweened animation are motion and shape.
CREATE ANIMATION
⁄ Click to select a keyframe. ¤ Draw an object on the › Click Insert ➪ Create ˇ Click in the frame in
Stage or drag an instance Motion Tween. which you want the
from the Library. animation to end.
■ If you draw an object on
‹ Click to select the the Stage, Flash automatically
Keyframe in which you converts the object to a
placed the object. symbol and names the object
Tween1.
14
3657-5 Ch01.F 1/31/02 12:16 PM Page 15
FIELD FUNCTION
Label Give a label name to a frame.
Tweening Select a tween type of None, Motion, or Shape.
Scale Select if tweening the size of the object.
Easing Type -1 to -100 to start the tween slowly and accelerate at the end.
Type 0 to 100 to start the tween rapidly and decelerate at the end.
Rotate Select CW to rotate the object clockwise. Select CCW to rotate the object counter
clockwise. Select Auto to rotate object once in the direction requiring the least motion.
Frame F5
FLASH ACTIONSCRIPT
ADD SOUND
ou can add spice to your Flash movie by adding sound. In the Sound panel, you can choose a sound effect, a
ADD SOUND
Import... Ctrl+R
horn.wav
⁄ Click File ➪ Import. ■ You may need to click ■ Flash sends the sound file Á Click Play ( ) to hear the
to locate your sound file if it to the Library. sound.
■ The Import dialog box does not appear in the Import
appears. window. › Click to open the ‡ Drag the sound onto the
Library. Stage.
¤ Click to select your sound ‹ Click Open to open the
file. file. ˇ Click the sound name to ■ The sound appears on the
select the sound. Timeline.
16
3657-5 Ch01.F 1/31/02 12:16 PM Page 17
EFFECT DESCRIPTION
None No sound effect.
Left Channel The sound plays in the left channel only.
Right Channel The sound plays in the right channel only.
Fade Left to Right The sound gradually shifts from the left channel to the right channel.
Fade Right to Left The sound gradually shifts from the right channel to the left channel.
Fade In The volume of the sound gradually increases.
Fade Out The volume of the sound gradually decreases.
Custom Lets you create channel and fade effects.
Panels
Sound
17
3657-5 Ch01.F 1/31/02 12:16 PM Page 18
FLASH ACTIONSCRIPT
⁄ Create an animation. ‹ Click Edit ➪ Copy Frames ˇ Click Insert ➪ New ‡ Click to select Movie Clip
to copy the frames you Symbol to open the Symbol as the behavior („ changes
Note: See page 14 for information on selected. Properties dialog box. to ´).
creating an animation.
› Deselect your selection by Á Type a symbol name. ° Click OK.
¤ On the Timeline, select clicking anywhere on the
every frame you want to Stage. ■ Flash changes to the
include in the movie clip. symbol-editing mode.
18
3657-5 Ch01.F 1/31/02 12:16 PM Page 19
Creating an animated button is easy. Start by To distinguish between instances of a movie clip, you
creating your animation. Convert your animation must give each instance a name. To reference a
to a movie clip. Create a button. Use the animation movie clip in ActionScript, in most cases the movie
to define the Up, Over, Down, or Hit state. clip must have a name. To name a movie clip instance,
use the Arrow tool to select the instance, click
To select everything on every layer, click Edit ➪
Window ➪ Panels ➪ Instance to open the Instance
Select All on the menu. To deselect everything
panel, and then type a name in the Name field.
on every layer, click Edit ➪ Deselect All. To select
a layer, click the layer name. To select a frame,
click in the frame. To select a block of frames,
click in the start frame, hold down the shift key,
and then click in the end frame. To add to a
selection, hold down the shift key as you make
your selection.
· Click in Frame 1 of Layer ‚ Click Edit ➪ Paste Frames. ■ Flash pastes the frames — Click the Scene button to
1 on the Timeline. you copied into the Timeline exit the symbol-editing mode.
of the movie clip symbol.
■ Your movie clip appears in
the Library.
19
3657-5 Ch01.F 1/31/02 12:16 PM Page 20
FLASH ACTIONSCRIPT
input text boxes to request information from the user. Your You can use embedded or device fonts. When you use a
text box can be single line, fixed width, or editable. Single font that you have installed on your system, Flash embeds
line text boxes expand as you type. A round handle in the the font in the movie. Flash does this to ensure that the font
upper-right corner of a text box indicates that the box is a will display properly as the movie plays. As an alternative to
single line text box. Fixed width text boxes wrap words as an embedded font, you can use a device font. When using
you type. A square handle in the upper right corner of a device fonts, Flash uses the font on the local computer that
text box indicates that the text box is a fixed width text box. most closely matches the device font you chose. Because
Editable text boxes let you accept user input or dynamically Flash does not embed device fonts, they reduce your file
display text. A square or round handle in the bottom right size. Also, if the point size of your font is less than 10, using
corner of the text box indicates that the text box is an a device font can yield a crisper, sharper text. Flash supplies
editable text box. you with three device fonts: _sans, _serif, and _typewriter.
Options...
⁄ Click Text ➪ Options to ■ The Text Options panel ¤ Click to select Static
open the Text Options panel. appears. Text from the drop-down list.
20
3657-5 Ch01.F 1/31/02 12:16 PM Page 21
You can make your text selectable. Making text You can set the alignment, margins, indents, and
selectable enables the user to click and drag to spacing of your text boxes. You use alignment to
highlight text in your movie. align your text with the left edge, right edge, or
center of the text box. You can also use the
You antialias your text to smooth the edges. To
alignment to justify your text. Use the margin
antialias, click View ➪ Antialias on the menu.
settings to specify the amount of space from the
Antialiasing works best with larger fonts. If you
edge of the text box and where the text begins or
have large amounts of text, antialiasing can slow
ends. Use indent to specify where the first line of
down the playback of your movie.
text for each paragraph will begin. Use spacing to
set the amount of space between each line of text.
To set the alignment, margins, indents, and spacing,
click Text ➪ Paragraph from the menu.
Options...
‹ Click the Text tool ( ) to Note: See page 23 for information on ‡ Click Text ➪ Option to set ° Click Use Device Fonts to
select it. creating a dynamic text box, and page your text options. use device fonts.
22 for information on creating an input
› Click in the color box text box. ■ The Text Options panel · Click Selectable to make
( ) to select the color of appears. text selectable.
your font. Á Type your text.
■ The text appears on the
ˇ Click the Stage for a single Stage.
line text box or click the
Stage and drag for a fixed
width text box. 21
3657-5 Ch01.F 1/31/02 12:16 PM Page 22
FLASH ACTIONSCRIPT
Y the user. You will frequently use input text boxes when
creating forms. Using the Text options panel, you
associate each input text box with a variable. Variables store
of the box. If you do not select Word Wrap, Flash will move
to a new line when the user presses Enter. Select Password
to have asterisks display as the user types. This option
information. To learn more about variables, see Chapter 4. enables you to create a form with password protection.
Flash assigns the information the user types in an input text
box to the variable you specify. You can use ActionScript to Select HTML to save the formatting with the HTML tags.
read the user input and respond to the user. Select Border/Bg to display a background behind and a
border around the text. Type the name you want to give the
When creating an input text box, you can set several variable in the Variable field. Type the maximum number of
options in the Text options panel. In the drop-down list, characters the user can enter in the Max. Chars. field. Click
select from Single Line, Multiline, or Password. Select Single one or more Embed Fonts buttons to specify which font
Line to display user input on a single line. If the text is too characters Flash should embed.
long to fit in the text box, the text box will scroll. Select
Multiline and check the Word Wrap box to display the user
Options...
⁄ Click the Text tool ( ). ‹ Click Text ➪ Options to ■ The Text Options panel ‡ Click to select HTML,
open the Text Options panel. appears. Border/Bg, or Word Wrap.
¤ Click to select a
font color. › Click to select Input ° Click the Stage for a single
Text from the drop-down list. line text box or click the
Stage and drag for a fixed
ˇ Click to select from width text box.
Single Line, Multiline, or
Password. ■ The text box appears on
the Stage.
22 Á Type a variable name.
3657-5 Ch01.F 1/31/02 12:16 PM Page 23
Options...
⁄ Click . ‹ Click Text ➪ Options to ■ The Text Options panel ‡ Click to select HTML,
open the Text Options panel. appears. Border/Bg, or Selectable.
¤ Click to select a
font color. › Click to select ° Click the Stage for a
Dynamic Text from the single line text box or click
drop-down list. the Stage and drag for a
fixed width text box.
ˇ Click to select Single
Line or Multiline. ■ The text box appears on
the Stage.
Á Type a variable name. 23
3657-5 Ch01.F 1/31/02 12:16 PM Page 24
FLASH ACTIONSCRIPT
⁄ In the Toolbox list, click ¤ Double-click a statement ‹ Click the Add a Statement › Click a statement from the
the category name to display to select it. button ( ) to display drop-down menu.
the statements in the statement categories.
category.
24
3657-5 Ch01.F 1/31/02 12:16 PM Page 25
You can name your file anything you want. Use AS for the file
extension. The filename.as argument represents the relative
path to the file. If the movie file and the ActionScript text file
are in the same folder, the path is filename.as. If the
ActionScript text file is in a subfolder, the path to the file is
foldername/filename.as. When you test, publish, or
export your movie, the text file must be present. ActionScript
replaces the #include statement with the contents of the file.
ˇ Click the Delete a Á Click to open the ■ The Parameters panel ° Click this Change the
Statement button ( ) to Parameters panel. appears at the bottom of the Statement Order button ( )
delete a statement. panel. to move a statement down.
■ The Action list displays the
script.
‡ Click this Change the CONTINUED
Statement Order button ( )
to move a statement up. 25
3657-5 Ch01.F 1/31/02 12:16 PM Page 26
FLASH ACTIONSCRIPT
gotoAndPlay (1);
26
3657-5 Ch01.F 1/31/02 12:16 PM Page 27
— Click to expand the ± Click to change modes ■ Use the Actions panel to
Action list. or select options. add ActionScript.
27
3657-5 Ch01.F 1/31/02 12:16 PM Page 28
FLASH ACTIONSCRIPT
on (release) {
Actions Ctrl+Alt+A
⁄ Use the Arrow tool ( ) to ¤ Click Window ➪ Actions ■ The Object Actions panel ■ ActionScripts defaults to
select a button. to open the Object Actions appears. on(release).
panel.
‹ Click Basic Actions. ˇ Click a check box to
select a handler.
› Double-click On Mouse
Event to select a handler. ■ Use handlers to specify
when Flash should execute
ActionScript.
28
3657-5 Ch01.F 1/31/02 12:17 PM Page 29
onClipEvent (load) {
Actions Ctrl+Alt+A
onClipEvent
⁄ Use to select the ¤ Click Window ➪ Actions ■ The Object Actions panel › Double-click onClipEvent
movie clip. to open the Object Actions appears. to select the onClipEvent
panel. action.
‹ Click Actions.
ˇ Click to select a handler
(„ changes to ´).
■ Use handlers to specify
when Flash should execute
ActionScript.
29
3657-5 Ch01.F 1/31/02 12:17 PM Page 30
FLASH ACTIONSCRIPT
Layer
Keyframe F6
⁄ Click Insert ➪ Layer to ■ A new layer appears above ‹ Click Insert ➪ Keyframe to
create a new layer. the active layers. create a keyframe.
¤ Click to select a frame.
30
3657-5 Ch01.F 1/31/02 12:17 PM Page 31
gotoAndPlay (1);
Actions Ctrl+Alt+A
› Click Window ➪ Actions ˇ Click to select your ■ Use the Frame Actions
to open the Frame Actions actions. panel to enter your
panel. ActionScript.
31
3657-5 Ch01.F 1/31/02 12:17 PM Page 32
FLASH ACTIONSCRIPT
PUBLISH MOVIES
ublishing your movie enables you to present it to your Selecting Generate Size Report creates a TXT file containing
PUBLISH MOVIES
⁄ Click File ➪ Publish ■ The Publish Settings dialog ¤ Click in the Type Boxes to › Click Use Default Names
Settings to open the Publish box opens. select file types ( changes to use the default filenames.
Settings dialog box. to ).
ˇ Click the Flash tab to set
‹ Type in the Filename field the options for the SWF file.
to change the filename.
32
3657-5 Ch01.F 1/31/02 12:17 PM Page 33
Bottom Up Bottom Up
■ The Flash option screen ° If you selected Debugging ‚ Click to override current — Click Set to open the
appears. Permitted, enter a password if sound setting. Sound Setting dialog box for
you want. Stream or Event sounds.
Á Click to set the Load
Order. · Set the JPEG Quality using
the JPEG Quality slider.
‡ Click to select the
Generate Size Report, Omit
Trace Actions, Protect from CONTINUED
Import, or Debugging
Permitted options. 33
3657-5 Ch01.F 1/31/02 12:17 PM Page 34
FLASH ACTIONSCRIPT
You can select the version of Flash for which you want to The Publish Settings dialog box contains four playback
publish your movie. Actions available in Flash 5 may not be options. You can pause the movie until the user presses a
available in earlier versions. button or selects play from the shortcut menu. You can
have the movie loop when it reaches the last frame. You can
To play your movie in a Web browser, the movie must be display a shortcut menu when the user right-clicks in
part of an HTML document. Selecting HTML as the format Windows or control-clicks on a Macintosh. And, in
in the Publish Settings dialog box creates an HTML Windows, you can use device fonts if the font you selected
document that includes your Flash movie. is not available on the system of the user.
Flash provides you with several templates you can use to The Publish Settings dialog box also has options that enable
create your HTML document. Pressing the Info button, in you to set the quality, window mode, alignment, and scale.
Bottom Up
■ The Sound Settings dialog ¡ Click button to select a £ Click to select the ¢ Click the HTML tab to
box appears. Sample Rate. version of Flash for which display the HTML options.
you are authoring.
± Click to select a ™ Click OK to close the
Compression type. dialog box.
34
3657-5 Ch01.F 1/31/02 12:17 PM Page 35
■ The HTML options appear. ¶ Click Info for a description ■ The dialog box closes. º Click Publish to publish
of the template. your movie.
∞ Click to set the ª Click to select the
Template and Dimensions • Click OK to close the Quality, Window Mode, ■ Flash creates an HTML
options. HTML Template Info dialog HTML Alignment, Scale, or document that you can use to
box. Flash Alignment options. publish your movie on the
§ Click to select the Web.
Playback options ( changes
to ).
35
3657-5 Ch02.F 1/31/02 12:17 PM Page 36
FLASH ACTIONSCRIPT
Actions
Actions Ctrl+Alt+A
⁄ Select the frame, button, ¤ Click Window ➪ Actions ‹ Click Basic Actions. ■ The parameters panel
or movie clip to which you to open the Actions panel. opens.
want to add ActionScript. › Double-click Go To to
Note: If you associate an action with select the gotoAndPlay
■ This example uses a frame. a button or movie clip, the Object action.
Actions panel will open. If you
associate an action with a frame,
the Frame Actions panel will open.
36
3657-5 Ch02.F 1/31/02 12:17 PM Page 37
ˇ Click to select Scene 1. ■ A semicolon ends the Á Double-click Tell Target. ■ When you select
statement. Expression, Flash removes
■ Quotes enclose the literal ‡ Type sampleMC in the the quotes.
value. ■ Parentheses enclose the Target field.
arguments and a comma · Double-click Stop.
separates the arguments. ° Select Expression if your
entry is an expression. ■ Curly braces surround the
block of code.
37
3657-5 Ch02.F 1/31/02 12:17 PM Page 38
FLASH ACTIONSCRIPT
TEST A MOVIE
s you create your Flash movie, you will want to test Buttons and Enable Simple Frame Actions to test buttons
When you click File ➪ Save, Flash saves the movie you are
authoring in FLA format and appends an FLA extension to
To test your movie in the authoring environment, use the the filename. To view your animation on the Web, your file
commands on the Control menu. The Play option plays must be in its final format — the SWF format.
your movie. The Rewind option returns your movie to the
first frame. The Step Forward option moves your movie Not all animations and scripts work in the authoring
forward one frame. The Step Backward option moves your environment. You must export your movie to its final SWF
movie backward one frame. Loop Playback causes your format for some animations and scripts to work. Clicking
movie to play continuously. The Play All Scenes option Control ➪ Test Movie from the menu exports your movie to
causes all scenes to play. The Mute Sounds option causes its final format, creating an SWF file in accordance with the
your movie to play without sound. By default, Flash disables setting you choose in the Publish Settings dialog box, and
buttons and actions, enabling you to manipulate buttons, moves you to the test environment. Clicking Control➪Test
movie clips, and frames as you work. Select Enable Simple Scene from the menu also creates a SWF file. Control, Test
Scene enables you to test the current scene.
TEST A MOVIE
sun sun
⁄ Click Control to display ‹ Click Rewind to return to ˇ Click Step Backward to ‡ Click Enable Simple
the Control menu. the first frame. move backward one frame. Frame Actions and click Play
to test actions.
¤ Click Play to play your › Click Step Forward to Á Click Play All Scenes and
movie. move forward one frame. click Play to play all scenes. ° Click Enable Simple
Buttons to test buttons.
38
3657-5 Ch02.F 1/31/02 12:17 PM Page 39
You can play your movie using the Controller To test your movie in a Web browser, click File ➪
Toolbar. Click Window ➪ Toolbar ➪ Controller Publish Preview ➪ Default from the menu. Flash
in Windows or click Window ➪ Controller on opens your movie in your default Web browser.
the Macintosh to open the Controller bar. The
Stop button will stop your movie. The Rewind
button will return your movie to frame 1. Step
Back steps your movie back one frame. Play plays
your movie. Step Forward steps you forward one
frame. Go To End takes you to the last frame.
sun
· Click Mute Sounds or ‚ Click Test Movie or Test ■ This is the test environment. ■ In the test environment,
Play to have your movie Scene to move to the test you can test your movie in
play without sound. environment. its final form.
39
3657-5 Ch02.F 1/31/02 12:18 PM Page 40
FLASH ACTIONSCRIPT
⁄ Click Control ➪ Test ■ Your movie plays ¤ Click Debug and select the
Movie to move to the test automatically. modem speed at which you
environment. want to test your movie.
40
3657-5 Ch02.F 1/31/02 12:18 PM Page 41
When in the test environment, you can quickly You can choose to display the Status bar, Controller,
move to the Actions panel by clicking Window ➪ or Main toolbar by clicking Window ➪ Toolbars
Actions from the menu. from the menu. You can use the Controller to play,
rewind, step forward, step backward, or loop the
You can zoom in on your movie by clicking
playback of your movie. You can use the Main
View ➪ Zoom In. You can zoom out by clicking
toolbar to open a file. The Status bar displays the
View Zoom Out. Set your movie quality by
state of the Caps Lock and Num Lock keys.
clicking View ➪ Quality from the menu.
41
3657-5 Ch02.F 1/31/02 12:18 PM Page 42
FLASH ACTIONSCRIPT
ADD COMMENTS
dding comments to your Flash movie is essential when execute your script, Flash ignores comments. When you are
ADD COMMENTS
action
Actions Ctrl+Alt+A
⁄ Select the frame, button, ■ This example uses a frame. ‹ Click Actions. ˇ Type your comment.
or movie clip to which you
want to add ActionScript. ¤ Click Window ➪ Actions › Double-click comment.
to open the Actions panel.
42
3657-5 Ch02.F 1/31/02 12:18 PM Page 43
/* comment
* comment
* comment
*/.
action
Á Click to select the ‡ Click to close the ° Click Control ➪ Test Note: See page 38 for information
statements you want to Actions panel. Movie to move to the test on testing your movie.
execute. environment.
■ When you execute your
· Test your movie. script, Flash ignores
comments.
43
3657-5 Ch02.F 1/31/02 12:18 PM Page 44
FLASH ACTIONSCRIPT
STOP A MOVIE
s soon as you load a Flash movie, it begins to play and about naming movie clips, see Chapter 1. Targeting a movie
A continues to play unless you stop it. You can use the
stop action to stop a movie. The syntax for the stop
action is
clip means you designate the movie clip on which you want
ActionScript to perform an action. For more about targeting
movie clips, see Chapter 10.
stop(); . You can stop a movie or movie clip at any point. Movies
begin playing as soon as they load. You can stop a movie in
The stop action does not take any arguments, so it has no the first frame and have users press a button when they are
parameters. You can use the stop action to stop the main ready to start it. Or you can stop a movie at the end of a
Timeline or to stop a movie clip. If you want to stop the scene and create buttons to determine the next scene to
main Timeline, simply issue a stop action. If you want to play. You can assign the stop action to any button, frame,
stop a movie clip, the movie clip must be on the Stage, it or movie clip; however, you will most frequently use the
must have an instance name, and you must target the movie stop action when you want to use buttons to control the
clip. You name a movie clip in the Instance panel. For more action in your movie.
STOP A MOVIE
Actions
Actions Ctrl+Alt+A
⁄ Click the frame, button, or ■ This example uses a frame. ‹ Click Basic Actions. ■ The stop action appears
movie clip to which you want in the Action list.
to add ActionScript. ■ Flash movies begin playing › Double-click Stop.
upon load. ˇ Click to close the
Note: This example uses file stop.fla, Actions panel.
which you can find on the CD-ROM ¤ Click Window ➪ Actions
that accompanies this book. to open the Actions panel.
44
3657-5 Ch02.F 1/31/02 12:18 PM Page 45
Example:
on (release) {
tellTarget ("smallMC") { stop ();
}
}
Actions
Á Click Control ➪ Test ‡ Test your movie. ■ Your movie displays on the
Movie to move to the test screen. Because you added a
environment. Note: See page 38 for information stop action, the movie will
on testing your movie. not play.
45
3657-5 Ch02.F 1/31/02 12:18 PM Page 46
FLASH ACTIONSCRIPT
PLAY A MOVIE
hen you stop a movie or movie clip, it remains action with tellTarget to start a movie clip. For more on
play(); . instanceName.play();
The play action does not take any arguments so it has no Use the instanceName argument to specify the movie clip
parameters. You can use the play action to start the main you want to play. The following example starts sampleMC:
Timeline or to start a movie clip. If you want to start the
main Timeline, simply issue a play action. If you want to sampleMC.play();
start a movie clip, the movie clip must be on the Stage, it
must have an instance name, and you must target the movie You can start a movie or movie clip, at any point. For
clip. You name a movie clip in the Instance panel. For more example, you can start a movie or movie clip when the user
about naming movie clips, see Chapter 1. Targeting a movie clicks a button, when the movie reaches a specified frame,
clip means you designate the movie clip on which you want or when the user presses a key. You can assign the play
ActionScript to perform an action. You can use the play action to any button, frame, or movie clip.
PLAY A MOVIE
Basic Actions
Play Esc+pl
action
Actions Ctrl+Alt+A
⁄ Select the frame, button, Note: This example uses file play.fla, ‹ Click ➪ Basic Actions ➪
or movie clip to which you which you can find on the CD-ROM Play to select the play action.
want to add ActionScript. that accompanies this book.
■ This example uses a button. ¤ Click Window ➪ Actions
to open the Actions panel.
46
3657-5 Ch02.F 1/31/02 12:18 PM Page 47
Example:
on (release) {
tellTarget ("smallMC") { play ();
}
}
action
Test Movie Ctrl+Enter
■ The on (release) ■ The play action appears ˇ Click Control ➪ Test Note: See page 38 for information
handler defaults. in the Action list. Movie to move to the test on testing your movie.
environment.
› Click to close the ■ When you click the Start
Actions panel. Á Test your movie. Movie button, the movie
plays.
47
3657-5 Ch02.F 1/31/02 12:18 PM Page 48
FLASH ACTIONSCRIPT
gotoAndPlay(scene,frame); You can use an expression to identify the frame you want to
gotoAndStop(scene,frame); go to, for example, gotoAndStop(_currentframe + 10).
You use the scene argument to tell ActionScript the name The _current frame property retrieves the number of the
of the scene to which you want to send the playhead. The current frame. The example adds 10 to the current frame.
scene argument is optional. If you do not enter a scene It tells ActionScript to move 10 frames ahead and stop
name, the playhead goes to the specified frame in the the movie.
current scene. You use the frame argument to tell
You can use the GoTo action to create a loop by creating a
ActionScript the frame to which you want to send the
frame action that goes to a prior frame. By creating buttons
playhead. It is best to use a label instead of a frame number
that take users to a particular frame or scene, you can
to identify the frame. Labels move with frames, but frame
create movies in which users can jump from place to place.
numbers change if you add, remove, or change the location
action
Actions Ctrl+Alt+A
⁄ Click the frame, button, or ■ This example uses a button. ‹ Click ➪ Basic Actions ➪ ■ The on (release)
movie clip to which you want Go To to select the goto handler defaults.
to add ActionScript. ¤ Click Window ➪ Actions action.
to open the Actions panel.
Note: This example uses file
jump.fla, which you can find on
the CD-ROM that accompanies
this book.
48
3657-5 Ch02.F 1/31/02 12:18 PM Page 49
action
Scene 2
■ The GoTo action appears Á Type a frame number or · Click Control ➪ Test Note: See page 38 for information
in the Action list. label. Movie to move to the test on testing your movie.
environment.
› Click to select the ‡ Click to select Go to and ■ When you click the
scene to which you want Play ( changes to ). ‚ Test your movie. jump button, you move to
to go. the scene specified in the
° Click to close the goto action.
ˇ Click to select a frame Actions panel.
type.
49
3657-5 Ch02.F 1/31/02 12:18 PM Page 50
FLASH ACTIONSCRIPT
can use the toggleHighQuality action to enable users You can create an HTML file to publish your Flash movie on
to toggle antialiasing off and speed up the playback of the the Web. You can use Flash to create your HTML file by
movie. The syntax for toggleHighQuality is clicking File ➪ Publish Settings and using the Publish
Settings dialog box. If you click the HTML tab in the Publish
toggleHighQuality(); . Settings dialog box, you can set the Flash parameters for
the HTML document. Among the parameters you can set
The toggleHighQuality action does not take any is the quality parameter. The quality parameter
arguments, so it has no parameters. Turning off antialiasing specifies the level of antialiasing to use when your movie
affects all of the movies in the player. You cannot adjust the plays on the Web. You choose the level you want to use.
quality of a single movie or movie clip. If you assign You can also set the quality of your movie manually by
toggleHighQuality to a button, users can toggle using the $QU variable. If you do not specify a quality value
antialiasing on and off by clicking the button. If antialiasing when you create your HTML document, Flash uses the
is on, clicking the button turns it off. If antialiasing is off, default setting of High.
Basic Actions
Layer 1
Toggle High Quality Esc+tq
Actions Ctrl+Alt+A
⁄ Click the frame, button, or ■ This example uses a button. ‹ Click ➪ Basic Actions ➪
movie clip to which you want Toggle High Quality to select
to add ActionScript. ¤ Click Window ➪ Actions the toggleHighQuality
to open the Actions panel. action.
Note: This example uses file
quality.fla, which you can find on
the CD-ROM that accompanies
this book.
50
3657-5 Ch02.F 1/31/02 12:18 PM Page 51
OPTION DESCRIPTION
Low Favors playback speed over appearance. Never uses antialising.
Auto Low Begins with antialiasing turned off. If Flash Player determines
that the processor can handle antialiasing, Flash Player turns
on antialiasing.
Auto High Begins with antialiasing turned on. If the Flash Player
determines that the processor cannot handle antialiasing,
Flash Player turns off antialiasing.
Medium Uses some antialiasing. Never smooths bitmaps.
High Uses antialiasing in every instance. This setting favors
appearance over playback speed. If the movie does not
contain any animation, it smooths bitmaps. If the movie does
contain animation, if does not smooth bitmaps.
Best All output is antialiased. This option does not consider
playback speed.
Layer 1
■ The on (release) › Click to close the ˇ Click Control ➪ Test Note: See page 38 for information
handler defaults. Action panel. Movie to move to the test on testing your movie.
environment.
■ The toggleHighQuality ■ Clicking the Quality
action appears in the Action Á Click the Quality button button toggles antialiasing
list. to test your movie. on and off.
51
3657-5 Ch02.F 1/31/02 12:18 PM Page 52
FLASH ACTIONSCRIPT
Basic Actions
Actions Ctrl+Alt+A
⁄ Click the frame, button, or ■ This example uses a ‹ Click ➪ Basic Actions ➪ ■ The getURL action
movie clip to which you want button. Get URL to select the getURL appears in the Action list.
to add ActionScript. action.
¤ Click Window ➪ Actions ■ The on (release)
Note: This example uses file url.fla, to open the Actions panel. handler defaults.
which you can find on the CD-ROM
that accompanies this book.
52
3657-5 Ch02.F 1/31/02 12:18 PM Page 53
You can also open a Web page by creating a link You can use getURL to create menus. Simply use
to the Web page in a text field. To create a link, the getURL action with a series of buttons. You can
use the Arrow tool to select a Text field. Click easily create many types of menus using Flash. For a
Text ➪ Character from the menu to open the rollover menu use the botton on rollover
Character panel. In the URL field, type the URL handler. Use can use the _visible property to
to which you want to link. make menu options visible on press or on rollover.
You can also display images on the screen when the
user rolls over the menu option.
› Type the URL to which Á Click to select the ° Click Control ➪ Test Note: See page 38 for information
you want to go. method you want to use to Movie to move to the test on testing your movie.
send variables. environment.
ˇ Click to select the ■ You can click the New
window or frame in which ‡ Click to close the · Click the New Web Page Web Page button to open a
the page will open. Actions panel. button to test your movie. Web page.
53
3657-5 Ch02.F 1/31/02 12:18 PM Page 54
FLASH ACTIONSCRIPT
Basic Actions
button
FSCommand Esc+fs
Actions Ctrl+Alt+A
⁄ Click the frame, button, or ■ This example uses a button. ‹ Click ➪ Basic Actions ➪ ■ The parameters panel
movie clip to which you want FSCommand to select the opens.
to add ActionScript. ¤ Click Window ➪ Actions fscommand action.
to open the Actions panel. ■ The on (release)
Note: This example uses file fsc.fla, handler defaults.
which you can find on the CD-ROM
that accompanies this book.
54
3657-5 Ch02.F 1/31/02 12:18 PM Page 55
fullscreen [true/false]
■ The fscommand action ˇ Type a command in the ° Open your Flash Player ■ You can click the Full
appears in the Action list. command field. and test your movie. Screen button to change your
movie to full screen mode.
› Click to select a Á Type an argument in the ■ Your movie plays.
command for a standalone argument field.
player.
‡ Publish your movie.
55
3657-5 Ch02.F 1/31/02 12:18 PM Page 56
FLASH ACTIONSCRIPT
Actions
startDrag Esc+dr
⁄ Click a button. ■ The startDrag action ‹ Type this in the Target ˇ Click on (release).
appears in the Action list. field.
¤ Click ➪ Actions ➪ ■ The handlers Parameters
startDrag to select the ■ The Parameters panel › Click Expression. panel opens.
startDrag action. opens.
56
3657-5 Ch02.F 1/31/02 12:18 PM Page 57
Actions
stopDrag Esc+sd
Á Click to select Press ( ‡ Click to deselect Release ° Click ➪ Actions ➪ · Click on (release).
changes to ). ( changes to ). stopDrag.
■ The handlers parameters
■ The stopDrag action panel opens.
appears in the Action list.
CONTINUED
57
3657-5 Ch02.F 1/31/02 12:18 PM Page 58
FLASH ACTIONSCRIPT
Convert to Symbol... F8
Layer
Symbol 6
‚ Click to select Roll Out — Click to close the ± Click Insert ➪ Convert to ¡ Type a symbol name.
( changes to ). Actions panel. Symbol to open the Symbol
Properties dialog box. ™ Click OK.
■ On Release is preselected.
58
3657-5 Ch02.F 1/31/02 12:18 PM Page 59
Example:
on (press) {
startDrag (this, false, 0, 0, 50, 50);
}
on (release) {
stopDrag ();
}
Layer 1
£ Click Control ➪ Test Note: See page 38 for information ¢ Click on the instance and ■ Your movie clip is
Movie to move to the test on testing your movie. drag to test your movie. draggable.
environment.
59
3657-5 Ch02.F 1/31/02 12:18 PM Page 60
FLASH ACTIONSCRIPT
PRINT A MOVIE
ou can use the print and printAsBitmap actions to frame. Use bmovie to tell Flash to use the area of the
PRINT A MOVIE
Actions
Layer 1
Actions Ctrl+Alt+A
Print Esc+pr
⁄ Click the frame, button, or ■ This example uses a ‹ Click ➪ Actions ➪ ■ The on (release)
movie clip to which you want button. Print to select the print handler defaults.
to add ActionScript. action.
¤ Click Window ➪ Actions ■ The print action appears
to open the Actions panel. in the Action list.
60
3657-5 Ch02.F 1/31/02 12:18 PM Page 61
Layer 1
As vectors
› Click to select a print Á Click to select a ° Click Control ➪ Test ■ You can print your movie
method. bounding method. Movie to move to the test from the test environment by
environment. right clicking in Windows or
ˇ Click to select a print ‡ Click to close the by Control-clicking on a
location. actions panel. · Print your movie. Macintosh to bring up the
Note: See page 38 for information context menu. Select Print
on testing your movie. from the menu.
61
3657-5 Ch02.F 1/31/02 12:18 PM Page 62
FLASH ACTIONSCRIPT
action
Actions Ctrl+Alt+A
⁄ Click the frame, button, or ■ This example uses a frame. ‹ Click Basic Actions to ˇ Click to select a scene.
movie clip to which you want open the Basic Actions
to add ActionScript. ¤ Click Window ➪ Actions category. Á Click to select a frame
to open the Actions panel. type.
› Double-click If Frame
Is Loaded to select the ‡ Click to select a frame.
ifFrameLoaded action.
62
3657-5 Ch02.F 1/31/02 12:18 PM Page 63
Frame 1
ifFrameLoaded(60)
gotoAndPlay(3);
Frame 2
gotoAndPlay(1);
action
° Double-click Go To. — Click to select a frame. ± Click Control ➪ Test ¡ Test your movie.
Movie to move to the test
· Click to select a scene. ■ Your movie plays after the environment. Note: See page 38 for information
specified number of frames on testing your movie.
‚ Click to select a frame have loaded.
type.
63
3657-5 Ch03.F 1/31/02 12:19 PM Page 64
FLASH ACTIONSCRIPT
Actions
Actions Ctrl+Alt+A
setProperty Esc+sp
64
3657-5 Ch03.F 1/31/02 12:19 PM Page 65
If you assign the above statement to a button, each click changes the
rotation property value. So, after the first click the equation reads:
sampleMC._rotation = sampleMC._rotation + 15
120 105 + 15
■ The default handler on ˇ Type the movie clip ° Move to the test · Click the button to test
(release) appears in the instance name. environment. your movie.
Action list.
Á Type the height to which Note: See page 38 for instructions ■ The height of the movie
■ The setProperty action you want to set the movie on how to test a movie. changes to the number of
appears in the Action list. clip. pixels you specified.
› Click to select the ‡ Click to select Expression
_height property. ( changes to ).
65
3657-5 Ch03.F 1/31/02 12:19 PM Page 66
FLASH ACTIONSCRIPT
NAME AN INSTANCE
o change the properties, or target a movie clip, the number. You cannot view assigned names in the Instance
You use the Instance panel to name instances of your movie The instance name argument is used to specify the
clips. When you select movie clip as the behavior for a instance for which you want to change the name. Use the
symbol, the instance panel includes a field that enables you newName argument to specify the new name. The following
to name the instance. changes the name of a movie clip from sampleMC to
yourMC: sampleMC._name = yourMC; .
If you do not name a movie clip instance, Flash assigns the
instance the name instance followed by a sequential
NAME AN INSTANCE
Layer 1 Layer 1
Panels
Instance Ctrl+I
⁄ Select the movie clip ¤ Click Window ➪ Panels ➪ ‹ Type the instance name. ■ You have named the movie
instance you want to name. Instance to open the Instance clip instance.
panel. › Click to close the
Instance panel.
66
3657-5 Ch03.F 1/31/02 12:19 PM Page 67
RENAME AN INSTANCE
Layer 1
Actions Ctrl+Alt+A
⁄ Select the frame, button, ■ This example uses a frame. ‹ Click Properties to open ■ The _name property
or movie clip to which you the Properties category. appears in the Actions list.
want to add ActionScript. ¤ Click Windows ➪ Actions
to open the Actions panel. › Double-click _name. ˇ Rename the movie clip.
■ The movie clip is renamed.
67
3657-5 Ch03.F 1/31/02 12:19 PM Page 68
FLASH ACTIONSCRIPT
ADJUST TRANSPARENCY
ou can use the _alpha property to set the argument to set the _alpha property to a value between 0
ADJUST TRANSPARENCY
Layer 1
Actions Ctrl+Alt+A
⁄ Select the frame, button, ■ This example uses a ‹ Click Properties to open ˇ Set the _alpha property.
or movie clip to which you button. the Properties category.
want to add ActionScript. ■ The instance name
¤ Click Window ➪ Actions › Double-click _alpha to precedes the property name.
Note: This example uses file to open the Actions panel. select the _alpha property.
alpha.fla, which you can find on the ■ The value follows the
CD that accompanies this book. ■ The default handler on property name.
(release) and _alpha
property appear in the Á Click to close the
Action list. Actions panel.
68
3657-5 Ch03.F 1/31/02 12:19 PM Page 69
You can change the _alpha property of symbols You can use the _alpha property in a tween to
and instances of symbols using the Effect panel. create a fading effect. Set the _alpha property at
To open the Effect panel, click Window ➪ the start position. Reset the _alpha property at the
Panel ➪ Effect on the menu. Select Alpha from end position. Flash will interpolate the values in
the drop-down menu. You can adjust the _alpha between causing the object to fade in or out. You
property of a an instance of a symbol to any can also change the _alpha property of text.
value from 0 to 100. Changing the _alpha However, you must first covert the text to a symbol.
property of an instance does not change the
symbol or other instances of the symbol.
Layer 1
■ The Flash menu is ‡ Click Control ➪ Test ° Click the button to test a ■ When you click the
available to you. Movie to move to the test movie. button, the transparency of
environment. the movie clip changes.
Note: See page 38 for instructions
on how to test a movie.
69
3657-5 Ch03.F 1/31/02 12:19 PM Page 70
FLASH ACTIONSCRIPT
particular frame, or a particular action has occurred. You can retrieve the _visible property value. When writing
script, you may want to retrieve the _visible property
Before you can change the _visible property of a movie value of a movie clip to determine what action ActionScript
clip instance, the instance must have a name. You use the should perform. For example, you can create a button that
Instance panel to name a movie clip instance. The syntax toggles the visibility of a movie clip on and off. If the
for the _visible property is _visibility value is true, the script will set the visibility
to false. If the _visibility value is false, the script will
instanceName._visible = BooleanValue; . set the _visibility to true. The syntax for retrieving the
_visible value is
The instanceName argument is used to specify the
instance name for which you want to change the _visible instanceName._visible; .
property. Use the BooleanValue argument to set the
_visible property to true or false. If you set the value Use the instanceName argument to specify the name of the
to true, the movie clip is visible. If you set the value to instance for which you want to change the _visible
false, the movie clip is not visible. property.
Button
Properties
Actions Ctrl+Alt+A
_visible
70
3657-5 Ch03.F 1/31/02 12:19 PM Page 71
Example:
on (release) {
x = getProperty ( starMC, _visible );
if (x == true) {
setProperty ("StarMC", _visible, false);
} else {
setProperty ("StarMC", _visible, true);
}
}
■ The default handler on ■ The instance name ˇ Move to the test Á Click the button to test
(release) appears in the precedes the property name. environment. your movie.
Action list.
■ The value follows the Note: See page 38 for instructions ■ When you click the
■ The _visible property property name. on how to test a movie. button, the movie clip
appears in the Action list. will no longer be visible.
› Set the _visible
property.
71
3657-5 Ch03.F 1/31/02 12:19 PM Page 72
FLASH ACTIONSCRIPT
Before you can set the value of the _rotation property of You can retrieve the current _rotation value. The syntax
a movie clip instance, the movie clip instance must have a for retrieving the rotation value is
name. Use the Instance panel to name your movie clip
instance. The syntax for setting the value of the _rotation instanceName. _rotation; .
property is
Use the instanceName argument to specify the instance
instanceName._rotation = value; . name of the movie clip you want to rotate. Omit the
instanceName argument to retrieve the rotation value of
The instanceName argument is used to specify the the movie.
Instance name of the movie clip for which you want to set
the value of the _rotation property. Omit the If you want to rotate a movie clip instance a specified
instanceName to set the rotation value for the movie. Use number of degrees from its current location, you can
the value argument to set the value of the _rotation retrieve the current value and add the number of degrees
property. you want to rotate the movie clip instance. For example,
sampleMC._rotation = sampleMC._rotation + 90;
Note that setting the rotation value to 90 rotates the movie rotates a movie clip 90 degrees from its current location.
clip instance 90 degrees from its original location, not its
Button
Properties
Actions Ctrl+Alt+A
_rotation
72
3657-5 Ch03.F 1/31/02 12:19 PM Page 73
You can use the Flash menu to rotate an object. Every symbol and instance of a symbol has a
Select an object, then click Modify ➪ Transform ➪ registration point. By default, the registration point
Rotate. Handles will appear around the object. is the center of the object. Objects rotate around
Use the mouse to grab the handles then drag to their registration point. To change the registration
rotate. Select an object and click Modify ➪ point, select the object, click Modify ➪ Transform ➪
Transform ➪ Rotate 90 CW to rotate an object Edit Center from the menu, and drag the
90 degrees clockwise. Select an object and click registration point to a new location.
Modify ➪ Transform ➪ Rotate 90 CCW to rotate
an object 90 degrees counterclockwise. You can
also use the Arrow tool rotate modifier to rotate
objects.
■ The default handler on › Set the _rotation ˇ Move to the test Á Click the button to test
(release) appears in the property. environment. your movie.
Action list.
■ The instance name Note: See page 38 for instructions ■ The movie rotates 10
Note: See page 28 for more precedes the property name. on how to test a movie. degrees each time you click
information on handlers. the button.
■ The value follows the
■ The _rotation property property name.
appears in the Action list.
73
3657-5 Ch03.F 1/31/02 12:19 PM Page 74
FLASH ACTIONSCRIPT
user types a number in the input field, the number is You can retrieve the _width property to obtain its current
assigned to the variable. That variable can be used to value. The syntax for retrieving the _width property is
determine the width of the movie clip. For more instanceName._width. Use the instanceName
information on variables, see pages 88 to 95. argument to specify the movie clip instance for which you
want to retrieve the _width property. Omit the Instance
Before you can change the _width property of a movie name if you want to retrieve the width of a movie clip.
clip, the movie clip must have a name. You use the Instance
panel to name a movie clip. The syntax for the _width As with all properties, remember, you can use
property is setProperty action to set the _width property value.
You can use the getProperty function to retrieve the
instanceName._width = value . current property value. For more information on
the setProperty action, see page 64. For more on
The instanceName argument is used to specify the name the getProperty function, see page 210.
of the movie clip for which you want to change the _width
Button
Properties
Actions Ctrl+Alt+A
_width
74
3657-5 Ch03.F 1/31/02 12:19 PM Page 75
Example:
on (keyPress "<Left>") {
widthV= getProperty ("block", _width );
setProperty ("block", _width, widthV + 10);
}
on (keyPress "<Right>") {
widthV= getProperty ("block", _width );
setProperty ("block", _width, widthV - 10);
}
■ The default handler on › Set the width of the movie ˇ Move to the test ‡ Click the button to test
(release) appears in the clip instance. environment. your movie.
Action list.
■ If you associate the variable Note: See page 38 for instructions ■ The width of the movie
Note: See page 28 for more with an input text box, you on how to test a movie. clip is set to the value entered
information on handlers. can obtain data from the user. in the input text box.
Á Type the number of pixels
■ The _width action Note: See page 22 for more on input to which you want to set the
appears in the Action list. text boxes, and pages 88 to 95 for width of the movie clip.
more on variables. 75
3657-5 Ch03.F 1/31/02 12:19 PM Page 76
FLASH ACTIONSCRIPT
Before you can change the _height property of a movie clip instanceName. _height; .
instance, the movie clip instance must have a name. You use
the Instance panel to name a movie clip instance. The syntax Use the instanceName argument to specify the name if
for the _height property is the movie clip for which you want to retrieve the _height
property. Omit the instanceName argument to retrieve
instanceName._height = value; . the height of the movie.
Actions Ctrl+Alt+A
⁄ Select the frame, button, ■ This example uses a ‹ Click Properties to open ■ The default handler on
or movie clip to which you button. the Properties category. (release) appears in the
want to add ActionScript. Action list.
¤ Click Window ➪ Actions › Double-click _height to
Note: This example uses file to open the Actions panel. select the _height property. Note: See page 28 for more
height2.fla, which you can find on information on handlers.
the CD that accompanies this book.
■ The _height property
appears in the Action list.
76
3657-5 Ch03.F 1/31/02 12:19 PM Page 77
ˇ Set the _height property ■ The instance name Á Move to the test ‡ Click the button to test
for a movie clip instance. precedes the property name. environment. your movie.
■ The value follows the Note: See page 38 for instructions ■ The size of the movie clip
property name. on how to test a movie. instance changes to the
height you set.
77
3657-5 Ch03.F 1/31/02 12:19 PM Page 78
FLASH ACTIONSCRIPT
The instanceName argument is used to specify the name of Use the instanceName argument to specify the instance
the movie clip instance. Omit the instance name argument to name of the movie clip for which you want to obtain the
set the _xscale property of a movie. Use the value _xscale. Omit the instanceName argument if you are
argument to specify the percent by which you want to retrieving the _xscale property of a movie.
change the width of the movie clip instance.
Button
Properties
Actions Ctrl+Alt+A
_xscale
78
3657-5 Ch03.F 1/31/02 12:19 PM Page 79
■ The default handler on › Set the _xscale property ˇ Move to the test Á Click the button to test
(release) appears in the for the movie clip instance. environment. your movie.
Action list.
■ The instance name Note: See page 38 for instructions ■ The width of the movie
Note: See page 28 for more precedes the property name. on how to test a movie. clip will increase to the
information on handlers. _xscale you specified.
■ The value follows the
■ The _xscale property property name.
appears in the Action list.
79
3657-5 Ch03.F 1/31/02 12:19 PM Page 80
FLASH ACTIONSCRIPT
Button
Properties
Actions Ctrl+Alt+A
_yscale
80
3657-5 Ch03.F 1/31/02 12:19 PM Page 81
Example:
on (press) {
setProperty ("block", _height, 0);
setProperty ("block", _width, 0);
}
on (release) {
block._yscale =100;
block._xscale =100;
}
■ The default handler on › Set the _yscale property ˇ Move to the test Á Click the button to test
(release) appears in the for a movie clip instance. environment. your movie.
Action list.
■ The instance name Note: See page 38 for instructions ■ The movie clip will scale
Note: See page 28 for more precedes the property name. on how to test a movie. to the size you specified.
information on handlers.
■ The value follows the
■ The _yscale property property name.
appears in the Action list.
81
3657-5 Ch03.F 1/31/02 12:19 PM Page 82
FLASH ACTIONSCRIPT
instanceName._x; .
Before you can change the _x property for a movie clip Use the instanceName argument to specify the name of
instance, you must give the movie clip an instance name. the instance for which you want to retrieve the _x property.
Use the Instance panel to name your movie clip. The syntax Retrieving the value is useful when you want to reset the
for the _x property is value of the _x property relative to its current location. For
example, the following moves a movie clip 10 pixels to the
instanceName._x = value; . right of its current location: sampleMC._x =
sampleMC._x +10; . You can associate this syntax with a
The instanceName argument is used to specify the instance button to move an object 10 pixels each time the user
name of the movie clip for which you want to set the _x presses a button. You can also associate this syntax with the
property. Omit the instance name if you want to set the _x onClipEvent (enterFrame) handler in a single frame
property for the movie. Use the value argument to specify movie to create continuous movement.
the number of pixels from the left border of the Stage you
want to locate the center of the movie or movie clip. The
left border of the Stage has a value of 0.
BoxButton
Properties
Actions Ctrl+Alt+A
_x
⁄ Select the frame, button, ■ This example uses a ‹ Click ➪ Properties ➪ _x.
or movie clip to which you button.
want to add ActionScript.
¤ Click Window ➪ Actions
Note: This example uses file x.fla, to open the Actions panel.
which you can find on the CD that
accompanies this book.
82
3657-5 Ch03.F 1/31/02 12:19 PM Page 83
■ The default handler on › Set the _x property. ˇ Move to the test Á Click the button to test
(release) appears in the environment. your movie.
Action list. ■ The instance name
precedes the property name. Note: See page 38 for instructions ■ Each time you click the
Note: See page 28 for more on how to test a movie. button, the movie clip
information on handlers. ■ The value follows the instance will move the
property name. number of pixels you
■ The _x property appears in specified.
the Action list.
83
3657-5 Ch03.F 1/31/02 12:19 PM Page 84
FLASH ACTIONSCRIPT
instanceName._y = value; . You will often use the _x and _y properties to move a
movie clip around the Stage. You can move a movie
The instanceName argument is used to specify the anywhere you want by specifying the coordinates. You can
instance name of the movie clip instance for which you use the _x and _y coordinates to create movement.
want to set the _y property. Omit the instanceName
Car
Properties
Actions Ctrl+Alt+A
_y
84
3657-5 Ch03.F 1/31/02 12:19 PM Page 85
Example:
on (press, keyPress "<Down>") {
y = getProperty ("block", _y );
setProperty ("block", _y, y + 3);
}
on (keyPress "<Up>") {
y = getProperty ("block", _y );
setProperty ("block", _y, y -3);
}
■ The default handler on ■ The expression ˇ Move to the test ‡ Click the button to test
(release) appears in the yellowCarMC._y environment. your movie.
Action list. retrieves the _y value.
Note: See page 38 for instructions ■ The text box displays
■ The _y property appears in ■ Associate the variable on how to test a movie. the _y value associated
the Action list. with a dynamic text box. This with the car.
enables you to display the Á Drag the movie clip.
› Assign the _y value to a value the variable returns to
variable. the user.
■ The variable name is yV.
85
3657-5 Ch03.F 1/31/02 12:19 PM Page 86
FLASH ACTIONSCRIPT
property to set the level of antialiasing you want to use in The syntax for the _highquality property is
your movie or you can use the _quality property to give
the user the ability to choose the quality level they want to _highquality = value; .
use as they play back the movie. You can use the value argument to specify the quality to
The syntax for the _quality property is which you want to set your movie clip. Choose from 2 -
best, 1 - high quality, or 0 - low quality. Like the _quality
_quality = value; . property, the _highquality property is global and
applies to all elements of the movie. You cannot use the
You use the value argument to specify the quality level. You _highquality property to set the quality of a movie clip
can choose from LOW, MEDIUM, HIGH, and BEST. You or other elements of a movie.
cannot set the quality level for specific elements of a movie.
You cannot set the quality for a movie clip. The quality Flash 5 deprecated the _highquality property; if you are
setting is global and applies to the entire movie. authoring for a Flash 5 environment, use the _quality
property instead.
You can also use the _highquality property to set the
level of antialiasing you want to use in your movie. As with
Layer 1
Properties
Actions Ctrl+Alt+A
_quality
86
3657-5 Ch03.F 1/31/02 12:19 PM Page 87
■ The default handler on › Set the _quality value. ˇ Move to the test Á Click the button to test
(release) appears in the environment. your movie.
Action list. ■ This is the value.
Note: See page 38 for instructions ■ The movie clip quality
■ The _quality property on how to test a movie. changes to the value you
appears in the action list. specified.
87
3657-5 Ch04.F 1/31/02 12:19 PM Page 88
FLASH ACTIONSCRIPT
INTRODUCTION TO VARIABLES
AND DATA TYPES
ou use variables to store information for later use. been assigned, whenever you use the variable name, the
DATA TYPES
In ActionScript, a variable can store the following data You do not have to define the variable type before
types: string, number, Boolean, movie clip, or object. A assigning a value to a variable. ActionScript examines
string is any sequence of characters consisting of any the expression and determines whether the variable is a
combination of letters, numbers, or punctuation marks. number, string, Boolean, object, or movie clip. If you
A number is a value on which you can perform change the value assigned to a variable, if necessary,
mathematical operations such as addition (+), ActionScript will automatically change the variable type.
subtraction (–), multiplication (*), division (/), modulo For example, when you assign x = 2;, ActionScript
(%), increment (++), or decrement (--). A Boolean is a evaluates the expression and determines that x is a
value that is either true or false. A movie clip is a symbol number. If you later change the assignment to x =
that can play Flash animation. Objects are used to "George";, ActionScript reevaluates the expression
manipulate data, sounds, and movie clips. and determines that x is a string.
SCOPE OF VARIABLES
ActionScript variables can be either global or local. A You use the assignment operator (=) to assign a value to
global variable can be referenced by any movie clip on a local variable. In the example shown here, the value
any timeline. Global variables are said to have an 25 is assigned to the local variable x.
unlimited scope. A local variable can be referenced only
within the curly braces that enclose its block of script. Example:
Local variables have a limited scope. Each variable must var x = 25
have a unique name within its scope. Each global You use the setVariables action or the assignment
variable must be unique within the movie. Each operator (=) to declare global variables. The following
local variable must be unique within the curly two examples set global variables and are equivalent.
braces that enclose it.
Declaring a variable lets ActionScript know that a Examples:
x = 25
variable exists even if no value has been assigned to it.
set(x,25);
You use the var statement to declare a local variable. In
the example shown here, the variable x is declared as a Global variables are often initialized in the first frame of
local variable. a movie. Initializing a variable consists of assigning the
initial value of the variable.
Example:
var x
88
3657-5 Ch04.F 1/31/02 12:19 PM Page 89
NAME VARIABLES
You are free to name your variables anything you like; * Your variable name must be unique within its scope.
however, when naming your variables, you must follow Global variables must be unique within the movie.
these rules: Local variables must be unique within the curly
* The first character of your variable name must be a braces that enclose them.
letter, underscore (_), or dollar sign ($). * Your variable name does not have to be in the
* Each subsequent character must be a number, letter, format of first word beginning with a lowercase
underscore (_), or dollar sign ($). letter and subsequent words beginning with capital
letters; however, that is the convention in this book.
* Your variable name cannot be a keyword.
If you develop a convention and use it consistently,
* Your variable name cannot be a Boolean literal. you will have an easier time debugging your code.
You can use variables to dynamically display text or to panel opens. Select Dynamic Text as the text type and
create forms. If you want to dynamically display text, enter the name you want to assign to your variable in
you start by using the Text tool to create a text box. the Variable field. If you would like a border around
After you have created the text box, click Window ➪ your field, select Border/Bg. Any information you assign
Panels ➪ Text Options on the menu. The Text Options to the variable displays on the screen.
If you want to create a form to capture user input, you the Variable field. Any information the user types in
start by using the Text tool to create a text box. The box the field is stored in the variable you assigned. You
will capture the user’s entry. After you have created the reference Input Text variables and Dynamic Text
text box, click Window ➪ Panels ➪ Text Options on the variables the same way that you reference any
menu. The Text Options panel opens. Select Input Text other variable.
as the text type and enter the name of your variable in
89
3657-5 Ch04.F 1/31/02 12:19 PM Page 90
FLASH ACTIONSCRIPT
Actions Ctrl+Alt+A
⁄ Select the frame, button, ¤ Click Window ➪ Actions ‹ Assign values to variables.
or movie clip to which you to open the Actions panel.
want to add ActionScript.
Note: This example uses file
variable.fla, which you can find on the
CD-ROM that accompanies this book.
90
3657-5 Ch04.F 1/31/02 12:19 PM Page 91
› Use the variables in an ˇ Move to the test Á Click the button to test ■ The script assigned the
expression. environment. your movie. values needed to execute the
script to variables. Your movie
■ If you associate the Note: See page 38 for instructions on will perform the actions the
variables with dynamic text how to test your movie. same way it would have if
boxes, you can display the you had used literal values.
values the variables return to
the user.
91
3657-5 Ch04.F 1/31/02 12:19 PM Page 92
FLASH ACTIONSCRIPT
ASSIGN AN OBJECT OR
MOVIE CLIP TO A VARIABLE
ctionScript divides data types into two categories: changes made subsequent to the time you originally
Actions Ctrl+Alt+A
⁄ Select the frame, button, ■ This example uses a ‹ Create an array. Note: See pages 164 to 175 for
or movie clip to which you button. information on working with arrays.
want to add ActionScript.
¤ Click Window ➪ Actions
Note: This example uses file object.fla, to open the Actions panel.
which you can find on the CD-ROM
that accompanies this book.
92
3657-5 Ch04.F 1/31/02 12:19 PM Page 93
PRIMITIVE DATA
DATA TYPE: TYPE: VARIABLES
ARRAY REFERENCE
v1 = "a," v2 = "b," v3 = c
y = v1+ v2 + v3 y = ["a","b","c"];
y returns: a,b,c y returns: a,b,c
v1 = "z,"; y[0] = "z";
output = y output = y
Output returns: a, b, c output returns: z, b, c
› Change the array. ˇ Assign the array to a ‡ Click the button to test ■ Because an array is a
variable. your movie. reference data type, the
■ This example changes one change you made to the array
of the elements in the array. Á Move to the test is reflected in the output.
environment.
■ If you associate the
variable with a dynamic text Note: See page 38 for instructions on
box, you can display the how to test your movie.
value the variable returns to
the user. 93
3657-5 Ch04.F 1/31/02 12:20 PM Page 94
FLASH ACTIONSCRIPT
LOAD VARIABLES
he loadVariables action reads data from an Use variableName to specify the name of the variable.
LOAD VARIABLES
Actions Ctrl+Alt+A
⁄ Select the frame, button, ■ This example uses a ‹ Load the variables. › Click to close the
or movie clip to which you button. Actions panel.
want to add ActionScript. ■ If you associate the
¤ Click Window ➪ Actions variables with dynamic text
Note: This example uses files load.fla to open the Actions panel. boxes, you can display the
and sales.txt, which you can find on values the variables return to
the CD-ROM that accompanies this the user.
book.
94
3657-5 Ch04.F 1/31/02 12:20 PM Page 95
RESULT:
RESULT:
temperature%3D%2B15
temperature=+15
ˇ Click Control ➪ Test Note: See page 38 for instructions on Á Click the button to test ■ When you click the
Movie to move to the text how to test your movie. your movie. button, the variables load.
environment.
■ Each of these boxes is a
dynamic text box.
95
3657-5 Ch04.F 1/31/02 12:20 PM Page 96
FLASH ACTIONSCRIPT
When using the string.charAt method, precede the You use the string.charCodeAt method to retrieve the
word charAt with the string or a variable that contains the code for the character at a specified location in a string. The
string from which you want to retrieve a character followed value returned is an integer from 0 to 65535. The
by a period. Place the index position of the character you string.charAt method and the string.charCodeAt
want enclosed in parentheses after the word charAt. Here method are the same, except the string.charCodeAt
is how you determine the index position. Starting from the method retrieves a code instead of the character.
left, the first character in a string has an index value of 0,
the second character has an index value of 1, the third The syntax for string.charCodeAt is
character has an index position of 2, and so on. In the stringValue.charCodeAt(index);. StringValue
example shown here, x is equal to c. represents the string from which you want to retrieve a
numeric code. Index represents the index position of the
x = "abcdefg".charAt(2) value for which you want to retrieve a code.
Actions Ctrl+Alt+A
96
3657-5 Ch04.F 1/31/02 12:20 PM Page 97
This script illustrates using charCodeAt. It You use the string.fromCharCode method to
retrieves the ASCII code of a character in the obtain the character assigned to the ASCII code.
variable phrase1. The variable sampleText is The string.fromCharCode method uses the
a dynamic text box. following syntax:
Example:
on (release) {
character = string.fromCharCode(userEntry);
}
› Extract a character. ˇ Move to the test Á Click the button to test ■ ActionScript extracts the
environment. your movie. character at index position 6.
■ This example extracts the
character at index position 6. Note: See page 38 for instructions on
how to test your movie.
■ If you associate the
variable with a dynamic text
box, you can display the
value the variable returns to
the user. 97
3657-5 Ch04.F 1/31/02 12:20 PM Page 98
FLASH ACTIONSCRIPT
Actions Ctrl+Alt+A
98
3657-5 Ch04.F 1/31/02 12:20 PM Page 99
› Extract a substring. ˇ Move to the test Á Click the button to test ■ ActionScript extracts the
environment. your movie. substring and displays it on
■ If you associate the screen.
variable with a dynamic text Note: See page 38 for instructions on
box, you can display the how to test your movie.
value the variable returns to
the user.
99
3657-5 Ch04.F 1/31/02 12:20 PM Page 100
FLASH ACTIONSCRIPT
CONCATENATE STRINGS
ou can use the string.concat method to join operator will add them. Here also, any spaces at the
CONCATENATE STRINGS
Actions Ctrl+Alt+A
⁄ Select the frame, button, ■ This example uses a ‹ Assign strings to variables.
or movie clip to which you button.
want to add ActionScript.
¤ Click Window ➪ Actions
Note: This example uses file concat.fla, to open the Actions panel.
which you can find on the CD-ROM
that accompanies this book.
100
3657-5 Ch04.F 1/31/02 12:20 PM Page 101
› Concatenate the strings. ■ If you associate the Á Type a word or phrase. ■ ActionScript concatenates
variables with input text the strings.
■ If you associate the boxes, you can obtain input ‡ Click the button.
variables with dynamic text from the user.
boxes, you can display the
values the variables return to ˇ Move to the test
the user. environment.
Note: See page 38 for instructions on
how to test your movie. 101
3657-5 Ch04.F 1/31/02 12:20 PM Page 102
FLASH ACTIONSCRIPT
Actions Ctrl+Alt+A
⁄ Select the frame, button, ■ This example uses a ‹ Obtain the length of a ■ If you associate the variable
or movie clip to which you button. string. with a dynamic text box, you
want to add ActionScript. can display the value the
¤ Click Window ➪ Actions ■ If you associate the variable returns to the user.
Note: This example uses file length.fla, to open the Actions panel. variable with an input text
which you can find on the CD-ROM box, you can obtain input ˇ Click to close the
that accompanies this book. from the user. Actions panel.
› Assign the results to a
variable.
102
3657-5 Ch04.F 1/31/02 12:20 PM Page 103
Example
/* Executes script when the user releases the button. */
on (release) {
/* Checks the length of the input text box studentID. */
if (studentID.length == 5 ) {
/* If the length of StudentID is fives character long, no
message is displayed in the dynamic text box errorMessage. */
errorMessage = “ “;
/* If the length of StudentId is not equal to five, a
message is displayed in the field errorMessage. */
} else {
errorMessage = “Your ID must be 5 characters long.”;
}
}
Á Click Control ➪ Test Note: See page 38 for instructions on ‡ Type a word or phrase. ■ ActionScript returns the
Movie to move to the test how to test your movie. number of characters in the
environment. ° Click the button to test text you typed.
your movie.
103
3657-5 Ch04.F 1/31/02 12:20 PM Page 104
FLASH ACTIONSCRIPT
SEARCH A STRING
sing the string.indexOf method, you can locate a you want to begin your search. For an explanation of index
SEARCH A STRING
Actions Ctrl+Alt+A
⁄ Select the frame, button, ■ This example uses a frame. ‹ Initialize the variable. ■ This part of this example
or movie clip to which you uses a button.
want to add ActionScript. ¤ Click Window ➪ Actions › Select the frame, button,
to open the Actions panel. or movie clip to which you
Note: This example uses file search.fla, want to add ActionScript.
which you can find on the CD-ROM
that accompanies this book.
104
3657-5 Ch04.F 1/31/02 12:20 PM Page 105
stringValue.lastIndexOf(value, start);
Frame 1:
startSearch = 36;
Button:
on (release) {
namesList = "George Bill Paul Bill Tom Bill Peter";
indexValue = namesList.lastIndexOf("Bill", startSearch );
startSearch = indexValue -1;
}
ˇ Assign a string to a ‡ Reset the variable you · Click the button to test ■ Each time you click the
variable. initialized in frame 1. your movie. button, ActionScript returns
the next index value for the
Á Find the index of a value. ° Move to the test string for which you are
environment.
■ Associate the variable with searching.
a dynamic text box. This Note: See page 38 for instructions on
enables you to display the how to test your movie.
value the variable returns to
the user. 105
3657-5 Ch04.F 1/31/02 12:20 PM Page 106
FLASH ACTIONSCRIPT
Result:
hello
Actions Ctrl+Alt+A
⁄ Select the frame, button, ■ This example uses a ‹ Convert a string to ■ Associate the variable with
or movie clip to which you button. lowercase. an input text box. This
want to add ActionScript. enables you to obtain input
¤ Click Window ➪ Actions ■ Associate the variable with from the user.
Note: This example uses file case.fla, to open the Actions panel. a dynamic text box. This
which you can find on the CD-ROM enables you to display the › Click to close the
that accompanies this book. value the variable returns to Actions panel.
the user.
106
3657-5 Ch04.F 1/31/02 12:20 PM Page 107
Example:
on (release) {
phrase1 = "JUMP OVER THE";
phrase2 = "MOON";
sampleText = phrase1.concat(userEntry.toUpperCase(), phrase2);
}
ˇ Click Control ➪ Test Note: See page 38 for instructions on Á Type a phrase in ‡ Click the button to test
Movie to move to the test how to test your movie. uppercase letters. your movie.
environment.
■ ActionScript converts
the uppercase letters to
lowercase letters.
107
3657-5 Ch04.F 1/31/02 12:20 PM Page 108
FLASH ACTIONSCRIPT
CONVERT STRINGS,
NUMBERS, AND BOOLEANS
CONVERT TO A STRING
CONVERT TO A BOOLEAN
108
3657-5 Ch04.F 1/31/02 12:20 PM Page 109
CONVERT TO A NUMBER
109
3657-5 Ch05.F 1/31/02 12:20 PM Page 110
FLASH ACTIONSCRIPT
UNDERSTAND PRECEDENCE
perators are characters that you can use to combine, In this example, x is equal to 9. ActionScript adds 1 plus 2
TYPES OF OPERATORS
Operators are characters that you use to combine, Binary operators are the most common type of
compare, or modify values. ActionScript calls the operator. Binary operators accept two operands. You
elements on which operators perform operands. There place binary operators between the operands. Plus (+)
are three types of operators: unary, binary, and ternary. and minus (–) are binary operators.
Unary operators accept one operand. Increment and Ternary operators accept three operands. The
decrement are unary operators. Most unary operators conditional operator (?:) is a ternary operator.
prefix the operand. The increment and decrement ActionScript evaluates expression1. If expression1
operators, however, can either prefix or suffix the is true, ActionScript returns expression2; if not
operand, depending on the operation you want to true, ActionScript returns expression3. For more on
perform. For more about increment and decrement, see the conditional operator, see Chapter 6.
pages 122 to 125.
110
3657-5 Ch05.F 1/31/02 12:20 PM Page 111
delete Deallocate object Right to left eq Equal (string version) Left to right
111
3657-5 Ch05.F 1/31/02 12:20 PM Page 112
FLASH ACTIONSCRIPT
You can use the plus + operator to add numbers, numeric You can use the plus operator with other numeric operators
variables and numbers, or numeric variables, for example: to perform complex mathematical calculations. You can add,
subtract, multiply, and divide in the same expression, but
z=x+2; you must be careful of precedence. Precedence controls the
z=x+y; order of calculation. For example, ActionScript performs
multiplication and division before addition and subtraction.
Concatenating joins two or more strings together to form a For an explanation of precedence, see page 110.
single string. You can use the plus + operator to
concatenate strings. If one operand in an expression is a
Operators
⁄ Open file add.fla on the ‹ Create a dynamic text Á Open the Object Actions ■ The Parameters panel
CD-ROM. box. panel. opens.
¤ Create input text boxes. › Create a button. ‡ Click ➪ Operators ➪ +.
ˇ Click to select the button.
112
3657-5 Ch05.F 1/31/02 12:20 PM Page 113
Example:
/* on (release) - tells Flash to start the
action when the user releases the mouse.*/
on (release) {
/* output - a dynamic text box. subtotal -
a dynamic text box. input - an input text box.
Number(input) - converts the value of input to
a number. */
/* The value of number(input) plus the
value of subtotal are added together and
assigned to output.*/
output = Number(input) + subtotal;
/* subotal - a dynamic text box. The
current value of Number(input) + the current
value of subtotal are added together and
assigned to subtotal. */
subtotal = Number(input) + subtotal;
/* Resets the value of input to 0.*/
input = 0;
}
° Type the mathematical ■ This example adds inputA · Move to the test — Click the button to
expression. to inputB. environment. calculate.
Note: See page 38 for instructions ■ ActionScript adds the two
on how to test your movie. input text boxes and displays
the results in the dynamic text
‚ Type in your values. box. 113
3657-5 Ch05.F 1/31/02 12:20 PM Page 114
FLASH ACTIONSCRIPT
Operators
⁄ Open file subtract.fla on ‹ Create a dynamic text Á Open the Object Actions ■ The Parameters panel
the CD-ROM. box. panel. opens.
¤ Create input text boxes. › Create a button. ‡ Click ➪ Operators ➪ –.
ˇ Click to select the button.
114
3657-5 Ch05.F 1/31/02 12:20 PM Page 115
Example:
/* on (release) - tells Flash to start the action when the user
releases the mouse button. */
on (release) {
/* output - a dynamic text box. subtotal - a dynamic text box.
input - an input text box. Number(input) - converts the value of
input to a number. */
/* The value of number(input) plus the value of subtotal are
added together and assigned to output. */
output = Number(input ) + subtotal;
/* subtotal - a dynamic text box. The current value of
Number(input) + the current value of subtotal are added together and
assigned to subtotal. */
subtotal = Number(input) + subtotal;
/* finalPrice - a dynamic text box. discount - a dynamic text
box. number(discount) - converts the value of discount to a number.*/
/* The value of number(discount) is subtracted from subtotal and
the result is assigned to finalPrice. */
finalPrice = subtotal - number(discount);
/* Resets the value of input to 0.*/
input = 0;
}
° Type the mathematical ■ This example subtracts · Move to the test ■ ActionScript subtracts
expression. inputB from inputA. environment. using the entries in the input
text boxes and displays the
Note: See page 38 for instructions on results in the dynamic text
how to test your movie. box.
‚ Click to calculate. 115
3657-5 Ch05.F 1/31/02 12:20 PM Page 116
FLASH ACTIONSCRIPT
Operators
*
⁄ Open file multiply.fla on › Create a button. Á Open the Object Actions ■ The Parameters panel
the CD-ROM. panel. opens.
ˇ Click to select the button.
¤ Create input text boxes. ‡ Click ➪ Operators ➪ *.
‹ Create a dynamic text
box.
116
3657-5 Ch05.F 1/31/02 12:20 PM Page 117
x=25.356; 428273.9856
y=4 *x +10;
z=(2+4)-(30*(-5*x));
output=z+y*z+(2+4);
° Type the mathematical ■ This example multiplies · Move to the test — Click to calculate.
expression. inputA by InputB. environment.
■ ActionScript multiplies
Note: See page 38 for instructions on using the entries in the input
how to test your movie. text boxes and displays the
results in the dynamic text
‚ Type in values. box.
117
3657-5 Ch05.F 1/31/02 12:20 PM Page 118
FLASH ACTIONSCRIPT
Operators
⁄ Open file divide.fla on the › Create a button. Á Open the Object Actions ■ The Parameters panel
CD-ROM. panel. opens.
ˇ Click to select the button.
¤ Create input text boxes. ‡ Click ➪ Operators ➪ /.
‹ Create a dynamic text
box.
118
3657-5 Ch05.F 1/31/02 12:20 PM Page 119
° Type the mathematical ■ This example divides · Move to the test — Click to calculate.
expression. inputA by inputB. environment.
■ ActionScript divides using
Note: See page 38 for instructions on the entries in the input text
how to test your movie. boxes and displays the results
in the dynamic text box.
‚ Type in values.
119
3657-5 Ch05.F 1/31/02 12:20 PM Page 120
FLASH ACTIONSCRIPT
Operators
⁄ Open file modulo.fla on › Create a button. Á Open the Object Actions ■ The Parameters panel
the CD-ROM. panel. opens.
ˇ Click to select the button.
¤ Create input text boxes. ‡ Click ➪ Operators ➪ %.
‹ Create a dynamic text
box.
120
3657-5 Ch05.F 1/31/02 12:20 PM Page 121
RESULT RESULT
410 14
° Type the mathematical ■ This example finds the · Move to the test — Click to calculate.
expression. modulo of inputA divided by environment.
inputB. ■ ActionScript finds the
Note: See page 38 for instructions on modulo using the entries in
how to test your movie. the input text boxes and
displays the results in the
‚ Type in values. dynamic text box.
121
3657-5 Ch05.F 1/31/02 12:21 PM Page 122
FLASH ACTIONSCRIPT
Example: x=4;
y=x++;
x=4; Result: y is equal to 4, x is equal to 5
y=++x; z=x;
Result: y is equal to 5, x is equal to 5 Result: x is equal to 5; z is equal to 5
z=x;
Result: x is equal to 5, z is equal to 5
Operators
++
⁄ Open file increment.fla on ‹ Create a button. ˇ Open the Object Actions ■ The Parameters panel
the CD-ROM. panel. opens.
› Click to select the button.
¤ Create dynamic text Á Click ➪ Operators ➪ ++.
boxes.
122
3657-5 Ch05.F 1/31/02 12:21 PM Page 123
Frame 1:
x = 0;
Button:
on (release) {
y = x ++;
}
y = ++ x ;
‡ Type the mathematical ■ This example ° Move to the test ■ ActionScript uses the
expression. increments x. environment. pre-increment operator
to set the value of x and y.
Note: See page 38 for instructions on ActionScript displays the
how to test your movie. results in the dynamic text
· Click to calculate. box. 123
3657-5 Ch05.F 1/31/02 12:21 PM Page 124
FLASH ACTIONSCRIPT
Y
Result: y is equal to 4, x is equal to 4
decrease the value of an expression by 1. This z=x;
operator is useful as a counter in a loop. For more Result: x is equal to 4, z is equal to 4
about loops, see Chapter 6. The syntax for the pre-
decrement operator is You can also use the post-decrement operator to decrease
the value of an expression. The post-decrement operator
--expression is similar to the pre-decrement operator and also can be
used as a counter in a loop. The post-decrement operator
The expression argument represents any numeric, subtracts 1 from the expression, assigns 1 minus the
variable, element in an array, or object property. The pre- expression to the expression, and returns the original
decrement operator subtracts 1 from the expression, value of the expression. The syntax for the post-
assigns 1 minus the expression to the expression, and decrement operator is
returns 1 minus the expression. If the expression x is
equal to 4, --x is equal to 3. The variable x is assigned 3. expression--
In the expression y=--x, y is equal to 3.
If the expression x is equal to 4, x-- is equal to 3. The
Example: variable x is assigned 3. In the expression y=x--, y is
equal to 4.
x=4;
y=--x;
Operators
--
⁄ Open file decrement.fla ‹ Create a button. ˇ Open the Object Actions ■ The Parameters panel
on the CD-ROM. panel. opens.
› Click to select the button.
¤ Create dynamic text Á Click ➪ Operators ➪ -- .
boxes.
124
3657-5 Ch05.F 1/31/02 12:21 PM Page 125
Frame 1:
x = 100;
Button:
on (release) {
y = x--;
}
y = x -- ;
‡ Type the mathematical ■ This example ° Move to the test ■ ActionScript uses the
expression. decrements x. environment. pre-decrement operator
to set the value of x and y.
Note: See page 38 for instructions on ActionScript displays the
how to test your movie. results in the dynamic text
· Click to calculate. box.
125
3657-5 Ch05.F 1/31/02 12:21 PM Page 126
FLASH ACTIONSCRIPT
When used with strings, the less than and greater than
returns the Boolean value true. If expression1 is greater
operators determine alphabetical order. Comparing the
than or equal to expression2, Flash returns the Boolean
string value of numbers produces unusual results. Use the
value false. The syntax for the less than expression is
Number function to convert strings to numbers before
expression1<expression2 doing a comparison. You often use these operators with
loops to repeat an action. To learn more about loops, see
Expression represents any number or string. Chapter 6.
You can use the greater than operator (>) to compare Flash 4 used the lt and gt operators to compare strings
two expressions and determine whether the first and the < and > operators to compare numbers. Flash 5
expression is greater than the second expression. If deprecates lt and gt. You should use < and > when
expression1 is greater than expression2, ActionScript authoring for a Flash 5 environment.
returns the Boolean value true. If expression1 is less
than or equal to expression2, ActionScript returns the
Boolean value false. The syntax for the greater than
operator is
<
⁄ Open file LessThan.fla on › Create a button. Á Open the Object Actions ■ This example compares
the CD-ROM. panel. x and y.
ˇ Click to select the button.
¤ Create input text boxes. ‡ Click Operators ➪ < to ■ ActionScript compares the
select Less Than. entries in the input text boxes
‹ Create a dynamic to determine if x < y.
text box. ■ The Parameters panel ActionScript displays the
opens. Boolean result in the
° Type the mathematical dynamic text box.
126 expression.
3657-5 Ch05.F 1/31/02 12:21 PM Page 127
expression1>=expression2
equal to expression2, ActionScript returns the Boolean
value true. If expression1 is greater than Expression represents any number or string.
expression2, Flash returns the Boolean value false. The When used with strings, the less than and equal to and the
syntax for less than or equal to is greater than and equal to operators determine alphabetical
expression1<=expression2 order. Comparing the string value of numbers produces
unusual results. Use the Number function to convert strings
Expression represents any number or string. to numbers before doing a comparison. You often use these
operators with loops to repeat an action. To learn more
You can use the greater than or equal to operator (>=) to about loops, see Chapter 6.
compare two expressions and determine whether the
first expression is greater than or equal to the second Flash 4 used the le and ge operators to compare strings
expression. If expression1 is greater than or equal to and the <= and >= operators to compare numbers. Flash 5
expression2, ActionScript returns the Boolean value deprecates le and ge. You should use <= and >= when
true. If expression1 is less than expression2, authoring for a Flash 5 environment.
answer = x <= y;
⁄ Open file LessThanE.fla on › Create a button. Á Open the Object Actions ■ This example compares
the CD-ROM. panel. two numbers.
ˇ Click to select the button.
¤ Create input text boxes. ‡ Click Operators ➪ <= to ■ ActionScript compares the
select Less Than Or Equal To. entries in the input text boxes
‹ Create a dynamic to determine if x <= y.
text box. ■ The Parameters panel ActionScript displays the
opens. Boolean result in the
° Type the mathematical dynamic text box.
expression. 127
3657-5 Ch05.F 1/31/02 12:21 PM Page 128
FLASH ACTIONSCRIPT
Operators
==
⁄ Open file equality.fla on › Create a button. Á Open the Object Actions ‡ Click ➪ Operators ➪ ==.
the CD-ROM. panel.
ˇ Click to select the button. ■ The Parameters panel opens.
¤ Create input text boxes.
‹ Create a dynamic
text box.
128
3657-5 Ch05.F 1/31/02 12:21 PM Page 129
Flash 4 used <> as the inequality operator. The You should not confuse the ==
characteristics of the <> operator and the != operator with the = operator. The
operator are the same. The <> operator uses this = operator assigns the expression
syntax: on the right side of the operator
to the variable on the left side of
expression1<>expression2
the operator. The == operator
Flash 5 deprecates <>. You should use != if you compares two values and returns
are authoring for a Flash 5 environment. true if they are equal, false if they
are not.
You will frequently use the equality and
inequality operators with the if action. Together,
the if action and the equality operators give you
the ability to evaluate whether a condition is
true or false and to perform an action based
on the results. For more on the if action, see
Chapter 6.
answ er = guess == 7;
° Type the mathematical ■ This example compares · Move to the test — Click to get a response.
expression. two values. environment.
■ ActionScript compares
Note: See page 38 for instructions on your entry to the value it has
how to test your movie. and displays in the dynamic
text box the Boolean true if
‚ Type a response. they are equal, false if they
are not equal.
129
3657-5 Ch05.F 1/31/02 12:21 PM Page 130
FLASH ACTIONSCRIPT
USING COMPOUND
ASSIGNMENT OPERATORS
ompound assignment operators provide you with This example uses the compound plus assignment operator:
The plus assignment operator adds the expression ActionScript supports all of the compound assignment
on the right of the operator, to the expression on the operators listed on the next page. When using a compound
left. For example, x+=5 adds 5 to x. The expression x+=5 assignment operator, the process is always the same: Use
also assigns 5+x to x. the value of the operand on the right side of the assignment
operator, and perform the operation indicated on the
The minus assignment operator subtracts the expression on the left side of the assignment operator.
expression on the right, from the expression on the
left and assigns the result to the expression on the left.
All of the other compound assignment operators work in a
similar fashion.
Operators
+=
Compound Assignment
⁄ Open file compound.fla ‹ Create a button. ˇ Open the Object Actions Á Click ➪ Operators ➪
on the CD-ROM. panel. Compound Assignment ➪ +=.
› Click to select the button.
¤ Create a dynamic text ■ The Parameters panel
box. opens.
130
3657-5 Ch05.F 1/31/02 12:21 PM Page 131
OPERATOR FUNCTION
+= Addition and assignment
-= Subtraction and assignment
*= Multiplication and assignment
/= Division and assignment
%= Modulo and assignment
<<= Bitwise shift right and assignment
>>= Shift right zero fill and assignment
^= Bitwise Xor and assignment
|= Bitwise Or and assignment
&= Bitwise And and assignment
output += 5;
‡ Type the mathematical ■ This example uses the ° Move to the test ■ ActionScript adds 5 to
expression. assignment operator. environment. output each time you click
the button and displays the
Note: See page 38 for instructions on result in the dynamic
how to test your movie. text box.
· Click to calculate.
131
3657-5 Ch05.F 1/31/02 12:21 PM Page 132
FLASH ACTIONSCRIPT
⁄ Open file logical.fla on › Create a button. Á Open the Object Actions ° Double-click Evaluate.
the CD-ROM. panel.
ˇ Click to select the button. ■ The Parameters panel
¤ Create input text boxes. ‡ Click Actions. opens.
‹ Create a dynamic text · Type the conditions to be
box. evaluated.
132
3657-5 Ch05.F 1/31/02 12:21 PM Page 133
EXPRESSION RETURN
true && true true
true && false false
false && true false
false && false false
true || true true
true || false true
false || true true
false || false false
!true false
!false true
o u t p u t = X & & Y;
‚ Click Operators. ■ The parameters panel ¡ Move to the test ™ Type equations.
opens. environment.
— Double-click &&. £ Click to get a response.
± Type the logical Note: See Chapter 1 for instructions on
operation. how to create a button. ■ ActionScript evaluates the
values and displays Boolean
true if they are true, false if
they are not true.
133
3657-5 Ch06.F 1/31/02 12:21 PM Page 134
FLASH ACTIONSCRIPT
actions give you the ability to have your script make similar on (release) {
decisions. if (guess == "7") {
message = "You are amazing!";
Say you need an ActionScript that requires the user to guess }
a number. If the user guesses the correct number, the script }
should send a message to the user that says, You are
amazing! You can use the if action to create your script. The user makes an entry in the input text box called guess.
The syntax for the if action is ActionScript compares the value of guess with 7. If guess
is equal to 7, ActionScript displays the message "You are
if (condition){ amazing."
statement;
} You use the if statement anytime you want to execute a
statement only if a specific condition is met. In the above
The if action takes two arguments, a condition and a example the script executes when guess is equal to 7. It
statement. The condition is an expression that could be set to execute when guess is greater than 7, less
evaluates to either true or false. The statement is the than 7, or some other condition.
instruction to be executed if the condition is true. The if
action evaluates the condition. If the condition is
Layer 1
⁄ Select the frame, button, Note: This example uses file if.fla, ¤ Open the Object Actions › Double-click if.
or movie clip to which you which you can find on the CD-ROM panel.
want to add ActionScript. that accompanies this book. ˇ Type the condition
‹ Click Actions. ActionScript will evaluate.
■ This example uses a button.
134
3657-5 Ch06.F 1/31/02 12:21 PM Page 135
OPERATOR USE
> Less than
< Greater than
== Equal to
>= Greater than or equal to
<= Less than or equal to
!= Not equal
Á Click Actions. ■ Associating a variable with · Move to the test — Click to get a response.
a dynamic text box enables environment.
‡ Double-click set variable. you to display the value the ■ ActionScript uses the if
Note: See page 38 for instructions action to evaluate your entry
° Assign a value to the variable returns to the user.
on how to test your movie. and responds with a message
variable. if the value you enter is the
‚ Type a number. value for which it is looking.
135
3657-5 Ch06.F 1/31/02 12:21 PM Page 136
FLASH ACTIONSCRIPT
movie can send a message to the user that says, Good job. You can use the if with else in you code to create a
If the entry is not five characters long, the movie can send a toggle button. With a toggle button you press the button to
message that says, Try again. turn something on and then you press the button again to
turn something off. You can create a sound toggle button.
The if action takes two arguments, a condition and a You press the button once and the sound starts, you press
statement. The condition is an expression that the button again and the sound stops.
evaluates to either true or false. The statement is an
instruction to execute if the condition is true. The syntax Your code would work something like this. You would set a
for the if action is value to false if the music is off. Then you would use an
if statement to test the value. If the value is false you
if (condition) { would turn the sound on and set the value to true. Your
statement(s); else statement would turn the music off whenever the
} value is not equal to false.
Layer 1
⁄ Select the frame, button, Note: This example uses file ¤ Open the Object Actions ‹ Set the if conditions.
or movie clip to which you ifelse.fla, which you can find on panel.
want to add ActionScript. the CD-ROM that accompanies
this book.
■ This example uses a button.
136
3657-5 Ch06.F 1/31/02 12:21 PM Page 137
expression1?expression2:expression3
Example:
id.length == 5?message = "Good
job":message = "Try again";
In the example, if the length of id is equal to 5,
the message field is assigned Good job. If the
length of id is not equal to 5, the message
field is assigned Try again.
› Set the else conditions. ■ Associating a variable with ˇ Move to the test ‡ Click to get a response.
a dynamic text box enables environment.
you to display the value the ■ ActionScript evaluates
variable returns to the user. Note: See page 38 for instructions your entry to see if it is five
on how to test your movie. characters long and responds
with Good job if it is, or Try
Á Test your movie by typing again if it is not.
a number.
137
3657-5 Ch06.F 1/31/02 12:21 PM Page 138
FLASH ACTIONSCRIPT
USING ELSE IF
f you have a situation with multiple possibilities and you false, it evaluates the next condition and executes it if it is
USING ELSE IF
Layer 1
⁄ Select the frame, button, Note: This example uses file ¤ Open the Object Actions ‹ Type the statement you
or movie clip to which you elseif.fla, which you can find on panel. want to evaluate using if.
want to add ActionScript. the CD-ROM that accompanies
this book.
■ This example uses a button.
138
3657-5 Ch06.F 1/31/02 12:21 PM Page 139
Example:
on (release) {
if (guess == "7") {
if (tries == 10) {
if (correct == 10) {
}
}
message = "AMAZING!";
}
}}
› Type your else ■ Associating a variable with ˇ Move to the test ‡ Click to get a response.
statements. a dynamic text box enables environment.
you to display the value the ■ ActionScript evaluates
variable returns to the user. Note: See page 38 for instructions your entry and responds with
on how to test your movie. a message.
Á Type a number to test
your movie.
139
3657-5 Ch06.F 1/31/02 12:21 PM Page 140
FLASH ACTIONSCRIPT
Layer 1
⁄ Select the frame, button, Note: This example uses file ¤ Open the Object Actions ‹ Type your while
or movie clip to which you while.fla, which you can find on panel. condition.
want to add ActionScript. the CD-ROM that accompanies
this book.
■ This example uses a button.
140
3657-5 Ch06.F 1/31/02 12:21 PM Page 141
You can also use the do...while action to You can place loops inside of loops or use if
create a loop. The do...while action creates a statements in loops. Your scripts can become
loop like the while action except the condition extremely complex. If you need to break out of
is evaluated at the end of the block of code so a loop, use the break action. The break action
that the loop always runs at least once. The instructs ActionScript to stop executing the loop
syntax for the do...while loop is and go to the next statement.
› Type your statements. ■ Associating a variable with ˇ Move to the test Á Click the button.
a dynamic text box enables environment.
you to display the value the ■ ActionScript concatenates
variable returns to the user. Note: See page 38 for instructions the word Hi four times.
on how to test your movie.
141
3657-5 Ch06.F 1/31/02 12:21 PM Page 142
FLASH ACTIONSCRIPT
Layer 1
⁄ Select the frame, button, Note: This example uses file for.fla, ¤ Open the Object Actions ‹ Type your for condition.
or movie clip to which you which you can find on the CD-ROM panel.
want to add ActionScript. that accompanies this book.
■ This example uses a button.
142
3657-5 Ch06.F 1/31/02 12:21 PM Page 143
When using a for loop, the init, condition, Your script can become extremely complex and you
and next arguments are optional. However, if may be resetting the condition within the loop. You
you do not include them in the for statement, can use the continue operator to break out of the
you must set them somewhere else in your loop, go to the condition operator and determine
script. If you omit the init argument, you must if the loop should continue.
still include the semicolon.
Example
i=1
for(; i<10, ++i) {
your statements;
]
› Type your statements. ■ Associating a variable with ˇ Move to the test Á Click the button.
a dynamic text box enables environment.
you to display the value the ■ ActionScript concatenates
variable returns to the user. Note: See page 38 for instructions the word Bye four times.
on how to test your movie.
143
3657-5 Ch07.F 1/31/02 12:22 PM Page 144
FLASH ACTIONSCRIPT
INTRODUCTION TO OBJECTS
bjects enable you to access information, or they The syntax for the new operator is
INTRODUCTION TO OBJECTS
Layer 1
⁄ Select the frame, button, Note: This example uses file ‹ Click Objects to open the ˇ Double-click new Object.
or move clip to which you object.fla, which you can find on the Objects category.
want to add ActionScript. ■ The new Object
CD that accompanies this book.
› Click Object to open the constructor appears in
¤ Click Window ➪ Actions Object category. the Action list.
to open the Actions panel.
144
3657-5 Ch07.F 1/31/02 12:22 PM Page 145
Example:
propertyValue = genericObject.weight;
Example:
genericObject.weight = 32
Á Assign the new object to · Move to the test ‚ Test your movie. ■ You have created a generic
a variable. environment. object with a temperature
property. The temperature
‡ Assign a property to the Note: See page 38 for instructions property automatically
object. on how to test your movie. displays on the screen
° Assign a property to a when you enter the test
variable. environment. 145
3657-5 Ch07.F 1/31/02 12:22 PM Page 146
FLASH ACTIONSCRIPT
movieClipName.method(arguments); .
can target instances of a movie clip. You do not need a
constructor to call MovieClip object methods; instead, The MovieClip object methods in the table below give
you the same functionality as standard actions.
146
3657-5 Ch07.F 1/31/02 12:22 PM Page 147
USING OBJECTS 7
The table that follows lists an example of each
of the movie clip methods that provide the same
functionality as standard actions.
ACTION EXAMPLE
MovieClip.duplicateMovieClip sampleMC.duplicateMovieClip(newSampleMC,1);
MovieClip.get.getURL sampleMC.getURL ("http://www.baycongroup.com",
"_blank", "GET");
MovieClip.gotoAndPlay sampleMC.gotoAndPlay (1);
MovieClip.gotoAndStop sampleMC.gotoAndStop (1);
MovieClip.loadMovie sampleMC.loadMovieNum ("load.swf", 1);
MovieClip.loadVariable sampleMC.loadVariablesNum ("sales.txt", 0);
MovieClip.nextFrame sampleMC.nextFrame();
MovieClip.play sampleMC.play();
MovieClip.prevFrame sampleMC.prevFrame();
MovieClip.removeMovieClip sampleMC.removeMovieClip();
MovieClip.startDrag sampleMC.startDrag();
MovieClip.stopDrag sampleMC.stopDrag();
MovieCip.unloadMovie sampleMC.unloadMovie();
The movie clip object includes several methods that enable you to perform actions
that are not standard actions on movie clips. You can apply many objects to a
movie by not specifying an instance name. The table that follows lists these actions:
147
3657-5 Ch07.F 1/31/02 12:22 PM Page 148
FLASH ACTIONSCRIPT
Layer 5 Layer 5
Linkage...
Library Ctrl+L
⁄ Click Window ➪ Library ¤ Click the item in the › Click Linkage on the
to open the Library. Library you want to attach. Options menu.
Note: This example uses file ‹ Click to select the
attachMC.fla, which you can find on Options menu.
the CD that accompanies this book.
148
3657-5 Ch07.F 1/31/02 12:22 PM Page 149
TYPE THIS:
RESULT:
sampleMC.attachMovie( "addMC","addMC2", 2 );
Layer 5
Save Ctrl+S
star
■ The Symbol Linkage Á Type a name in the ° Click File ➪ Save to save ■ You must save your file
Properties dialog box Identifier field. your file. after you set the Symbol
appears. Linkage property.
‡ Click OK.
ˇ Select Export This Symbol
as the Linkage type.
CONTINUED
149
3657-5 Ch07.F 1/31/02 12:22 PM Page 150
FLASH ACTIONSCRIPT
Layer 5
Actions Ctrl+Alt+A
· Select the frame, button, ‚ Click Window ➪ Actions — Attach the movie. ± Click to close the
or movie clip to which you to open the Actions panel. Actions panel.
want to add ActionScript.
150
3657-5 Ch07.F 1/31/02 12:22 PM Page 151
Example:
xlocation = 250;
ylocation = 150;
max = 4;
attachMovie("star","star0", 1);
setProperty ("star0", _x, xlocation);
setProperty ("star0", _y, ylocation);
for (i=2; i<max; i++) {
attachMovie("star","star" + i, i);
nameMC = "star"+i;
setProperty (nameMC, _x, xlocation + 100);
setProperty (nameMC, _y, ylocation + 50);
xlocation = xlocation + 100;
ylocation = ylocation + 50;
}
Layer 5
¡ Click Control ➪ Test Note: See page 38 for instructions ™ Click the button to test ■ ActionScript attaches the
Movie to move to the test on how to test your movie. your movie. movie clip when you click
environment. the button.
151
3657-5 Ch07.F 1/31/02 12:22 PM Page 152
FLASH ACTIONSCRIPT
GET BOUNDS
et us say you have placed a movie clip on the Stage or Use the MovieClip argument to specify the movie clip
MovieClip.getbounds(targetCoordinateSpace); .
GET BOUNDS
Layer 1
Actions Ctrl+Alt+A
⁄ Select the frame, button, Note: This example uses file ‹ Retrieve the bounds of a
or movie clip to which you getbounds.fla, which you can find on movie clip.
want to add ActionScript. the CD that accompanies this book.
¤ Click Window ➪ Actions
to open the Actions panel.
152
3657-5 Ch07.F 1/31/02 12:22 PM Page 153
You can use getbounds to help you place an attached movie clip
on the Stage. The example that follows attaches the star movie clip
and places two instances of the star movie clip on the Stage side
by side.
Example:
on (release) {
backMC.attachMovie( "star","star1", 2 );
border=_root.backMC.star1.getBounds(_root.backMC.star1);
xright = border.yMax;
backMC.attachMovie( "star","star2", 3);
setProperty (_root.backMC.star2, _x, xright);
}
› Assign each property to a ˇ Move to the test Á Click the button to test ■ When you click the button
variable. environment. your movie. the xMin, xMax, yMin, and
yMax properties display on
■ If you associate the variable Note: See page 38 for instructions the screen.
with a dynamic text box, you on how to test your movie.
can display the value the
variable returns to the user.
153
3657-5 Ch07.F 1/31/02 12:22 PM Page 154
FLASH ACTIONSCRIPT
SWAP DEPTHS
hen you place objects on the Stage, Flash gives The syntax for MovieClip.swapDepths is
SWAP DEPTHS
Picture 1
Actions Ctrl+Alt+A
⁄ Select the frame, button, Note: The script is associated with ‹ Set the handler to ˇ Increase the size of the
or movie clip to which you the button that has been converted on (press). movie clip.
want to add ActionScript. to a movie clip. To see the script in
the example file, double-click the › Swap the depth of the
Note: This example uses file movie clip and open the Actions movie clip.
swap.fla, which you can find on the panel.
CD that accompanies this book.
¤ Click Window ➪ Actions
to open the Actions panel.
154
3657-5 Ch07.F 1/31/02 12:22 PM Page 155
RESULT: RESULT:
Each time the user clicks the Each time the user releases the
button ActionScript swaps the mouse after clicking the button,
depth of pictureA with pictureB. pictureA moves to depth 100.
Á Set the handler to ° Move to the test · Click an image to test ■ When you click the image,
on (release). environment. your movie. the depth level swaps to the
top level and the image
‡ Reduce the size of the Note: See page 38 for instructions enlarges.
movie clip to its original size. on how to test your movie.
■ If you want to adjust the
depth of several symbols,
repeat steps 1 through 7 for
each symbol. 155
3657-5 Ch07.F 1/31/02 12:22 PM Page 156
FLASH ACTIONSCRIPT
Layer 1
Actions Ctrl+Alt+A
⁄ Select the frame, button, ■ This example uses a frame. ‹ Get the bytes loaded. ■ If you associate the variable
or movie clip to which you with a dynamic text box, you
want to add ActionScript. ¤ Click Window ➪ Actions can display the value the
to open the Actions panel. variable returns to the user.
156
3657-5 Ch07.F 1/31/02 12:22 PM Page 157
Example Frame 1
loaded = sailMC.getBytesLoaded();
total = sailMC.getBytesTotal();
percent = (loaded/total) * 100;
› Get the total bytes. Note: See page 38 for instructions Á Test your movie. ■ When the movie plays, the
on how to test your movie. bytes loaded and the total
ˇ Move to the test bytes automatically display
environment. on the screen.
157
3657-5 Ch07.F 1/31/02 12:22 PM Page 158
FLASH ACTIONSCRIPT
DETECT COLLISION
ou use the hitTest method to detect when two coordinates. Use the MovieClip argument to specify the
DETECT COLLISION
Actions Ctrl+Alt+A
⁄ Select the frame, button, ¤ Click Window ➪ Actions ‹ Set the handler to › Determine if the movie
or movie clip to which you to open the Actions panel. enterFrame. clips are in collision.
want to add ActionScript.
Note: This example uses hittest.fla,
which you can find on the CD that
accompanies this book.
158
3657-5 Ch07.F 1/31/02 12:22 PM Page 159
Example
onClipEvent (enterFrame) {
_root.collision =_root.redBox.hitTest( _root.blueDot);
}
You can use hitTest to determine if two objects are in collision. The script in this example
returns the Boolean value true to the variable collision when the user drags a blue dot
and causes it to collide with the x- and y-coordinates of a red dot. The example assigns the
variable collision to a dynamic text box. The blueDot is a draggable movie clip. The
redDot is a movie clip that motion-tweens across the Stage. The variables a and b are
assigned to dynamic text boxes. They capture and display the x- and y-coordinates of
redDot. You assign the action to blueDot.
Example
onClipEvent (enterFrame) {
_root.a = getProperty ( _root.redDot, _x );
_root.b = getProperty ( _root.redDot, _y );
_root.collision = _root.blueDot.hitTest(_root.a, _root.b, false );
}
ˇ Set the messageMC visible ‡ Move to the test ° Test your movie by ■ When one car touches the
property to true if the movie environment. dragging a car so that it other car, a message appears
clips are in collision. touches the other car. on the screen.
Note: See page 38 for instructions
Á Set the messageMC visible on how to test your movie.
property to false if the movie
clips are not in collision.
159
3657-5 Ch07.F 1/31/02 12:22 PM Page 160
FLASH ACTIONSCRIPT
Layer 1
Actions Ctrl+Alt+A
⁄ Select the frame, button, ■ This example uses a movie ‹ Set the event handler to ■ This causes the script to
or movie clip to which you clip. mouseMove. execute every time the user
want to add ActionScript. moves the mouse.
¤ Click Window ➪ Actions
Note: This example uses file to open the Actions panel.
localtoglobal.fla, which you can
find on the CD that accompanies
this book.
160
3657-5 Ch07.F 1/31/02 12:22 PM Page 161
Example
onClipEvent (mouseMove) {
position = new object();
position.x = _root._xmouse;
position.y = _root._ymouse;
this.globalToLocal(position);
_root.a = position.x;
_root.b = position.y;
}
› Create a new object. ‡ Assign the coordinates to ° Move to the test · Test your movie by
a variable. environment. dragging you mouse around
ˇ Assign the _xmouse and the screen.
_ymouse properties to the ■ If you associate the Note: See page 38 for instructions
x- and y- properties of the variables with dynamic text on how to test your movie. ■ The x and y coordinates of
new object. boxes, you can display the the mouse display on-screen.
values the variables return to As you drag the mouse, the
Á Use the current timeline the user. coordinates change.
as the object whose
coordinates you retrieve. 161
3657-5 Ch07.F 1/31/02 12:22 PM Page 162
FLASH ACTIONSCRIPT
CREATE AN ARRAY
n array is a list of values separated by commas. Arrays You can use the constructor new Array to create an array.
CREATE AN ARRAY
Layer 1
Actions Ctrl+Alt+A
⁄ Select the frame, button, Note: This example uses file ‹ Create an array. ■ If you associate the variable
or movie clip to which you createarray.fla, which you can with a dynamic text box, you
want to add ActionScript. find on the CD that accompanies can display the value the
this book. variable returns to the user.
¤ Click Window ➪ Actions
to open the Actions panel.
162
3657-5 Ch07.F 1/31/02 12:22 PM Page 163
You can create an array using the access You can add elements to the array.
operator. The script that follows is an example. It
includes strings, numbers, and Boolean values. TYPE THIS:
nameAge[6] = "Jen";
TYPE THIS: Name[7] = 13;
nameAge[8] = false;
nameAge = ["George",23,false,"Jane",45,true];
RESULT:
RESULT: The variable nameAge returns:
George,23,false,Jane,45,true,Jen,13,false
The variable nameAge returns:
George,23,false,Jane,45,true.
Layer 1
› Click Control ➪ Test ˇ Click the button to test ■ Clicking the button
Movie to move to the test your movie. retrieves the array.
environment.
CONTINUED
163
3657-5 Ch07.F 1/31/02 12:22 PM Page 164
FLASH ACTIONSCRIPT
trees[0] = "pine";
Array ();. The syntax for creating an empty array using trees[1] = "maple";
the array access operator is arrayName = [ ];. trees[2] = "birch";
To access or assign elements to an array, refer to the You can change the value of an element in an array by
element by using the syntax variableName[N];. Use assigning it a new value. This example assigns the value
variableName to specify the variable to which you assigned birch to the element trees[1]: trees[1] = "birch";
the array. Use the N argument to specify the index position of
You can access elements in an array. This example accesses
the element you want to access or assign. To add values to
element 1 of the trees array and assigns it to the variable
the array, specify the array name, followed by the index value
wood. In other words, it assigns maple to the variable
enclosed in the array access operator, and assign a value.
wood:
This example creates a new array: trees = new Array(); .
trees = ["pine","maple","birch"];
wood = trees[1];
Actions Ctrl+Alt+A
⁄ Select the frame, button, Note: This example uses file ‹ Use the new Array ■ If you associate the variable
or movie clip to which you createarray1.fla, which you can method to create your array. with a dynamic text box, you
want to add ActionScript. find on the CD that accompanies can display the value the
this book. › Use the set variables variable returns to the user.
action to assign values to your
¤ Click Window ➪ Actions array.
to open the Actions panel.
164
3657-5 Ch07.F 1/31/02 12:22 PM Page 165
You can change the values of the element You can create an empty array:
in an array. The array nameAge contains colors = new Array ();
George,23,false,Jane,45,true,Jen,13,false.
You can assign values to the empty array.
TYPE THIS: TYPE THIS:
nameAge[3] = "Tom"; colors[0] = "pink";
nameAge[4] = 56; colors[1] = "yellow"
NameAge[5] = false; colors[2] = "blue"
RESULT: RESULT:
The Array nameAge returns: pink,yellow,blue
George,23,false,Tom,56,false,Jen,13,false
ˇ Click Control ➪ Test Á Click the button to test ■ Clicking the button
Movie to move to the test your movie. retrieves the current values
environment. assigned to the array.
165
3657-5 Ch07.F 1/31/02 12:22 PM Page 166
FLASH ACTIONSCRIPT
The syntax for the Array.length property is Flash updates the length property automatically as you add
elements to an array. For example, if you use the new
arrayName. length; . Array constructor to create an empty array, the array has
a length of 0. If you add index position [1], the array has
Use the arrayName argument to specify the name of the a length of 1. If you add index position [2], the array has a
array whose length you want to obtain. length of 2. If you then add index position [10], the array
You can create an empty array of a specified length by using has a length of 10. Positions [3] through [9] do not have
the constructor new Array. The syntax for creating an values assigned to them; however, ActionScript reserves
empty array of a specified length is space for these elements.
Layer 1
Actions Ctrl+Alt+A
⁄ Select the frame, button, Note: This example uses file ‹ Create an array. ■ If you associate the variable
or movie clip to which you length.fla, which you can find on the with a dynamic text box, you
want to add ActionScript. CD that accompanies this book. can display the value the
variable returns to the user.
¤ Click Window ➪ Actions
to open the Actions panel.
166
3657-5 Ch07.F 1/31/02 12:22 PM Page 167
RESULTS:
RESULT:
The array will return: ,23,
The length property will return : 0
And the length property will return: 3
RESULT:
The array will return: ,,
And the length property will return: 3
› Find the length of the Note: See page 38 for instructions Á Click the button to test ■ Clicking the button
array. on how to test your movie. your movie. retrieves the array and the
array length.
ˇ Move to the test
environment.
167
3657-5 Ch07.F 1/31/02 12:22 PM Page 168
FLASH ACTIONSCRIPT
arrayName.push(v1, v2,...vN) .
ways to add a large number of elements to an array.
Here, also, use the arrayName argument to specify the
You use the push method to add elements to the end of an name of the array. Use arguments v1 to vN to specify the
array. The push method adds one or more elements to the elements you want to add.
end of the array and returns the new length.
You can use the concat method to create a new array
The syntax for the push method is by concatenating two or more arrays. The syntax for
the concat method is newArray = arrayName.
arrayName.push(v1,v2,...vN) . concat(v1,v2,...vN). Use newArray to specify the
name of the array you want to create by using the concat
Use the arrayName argument to specify the name of the method. Use arrayName to specify the array to which you
array to which you want to add elements. Use arguments want to concatenate. Use arguments v1 to vN to specify the
v1 to vN to specify the elements you want to add. arrays you want to concatenate to the array in the
arrayName argument.
You can also use the unshift method to add elements to
an array. The unshift method adds one or more elements
Actions Ctrl+Alt+A
⁄ Select the frame, button, Note: This example uses file ‹ Create an array. ■ If you associate the variable
or movie clip to which you pusharray.fla, which you can find on with a dynamic text box, you
want to add ActionScript. the CD that accompanies this book. can display the value the
variable returns to the user.
¤ Click Window ➪ Actions
to open the Actions panel.
168
3657-5 Ch07.F 1/31/02 12:22 PM Page 169
Example:
trees = ["pine","maple","birch"];
You can add elements to the beginning of the array using the unshift method.
lengthV = trees.unshift("pear","apple");
pear,apple,pine,maple,birch
Example:
trees = ["pine","maple","birch"];
fruit = ["apple","pear","peach"];
fruitTrees = trees.concat(fruit);
pine,maple,birch,apple,pear,peach
› Add elements. Note: See page 38 for instructions Á Click the button to test ■ Clicking the button
on how to test your movie. your movie. retrieves the elements of the
ˇ Move to the test array and the array length.
environment.
169
3657-5 Ch07.F 1/31/02 12:22 PM Page 170
FLASH ACTIONSCRIPT
to remove the last element from an array and retrieve its arrayName.splice(start,count,v1,v2,...vN); .
value.
Use the arrayName argument to specify the array in which
The pop method uses the following Syntax: you want to add, remove, or replace elements. Use the
start argument to specify the index value of the element
arrayName.pop() . where the insertion or deletion begins. Use the count
argument to specify the number of elements to delete. The
Use the arrayName argument to specify the name of the deletion includes the elements specified in the start
array. argument. If you do not want to delete any elements, use 0
as the count argument. Use v1 to vN to specify the values
You use the shift method to remove the first element to insert at the point specified in the start argument. The
from an array and return its value. The shift method uses splice method returns the deleted elements.
the following Syntax:
arrayName.shift() .
Layer 1
Actions Ctrl+Alt+A
⁄ Select the frame, button, Note: This example uses file ‹ Create an array. ■ If you associate the variable
or movie clip to which you poparray.fla, which you can find on with a dynamic text box, you
want to add ActionScript. the CD that accompanies this book. can display the value the
variable returns to the user.
¤ Click Window ➪ Actions
to open the Actions panel.
170
3657-5 Ch07.F 1/31/02 12:22 PM Page 171
TYPE THIS:
RESULT: trees = ["pine", "maple", "birch"];
trees.splice(1, 1, "apple", "pear");
The variable deleteV returns pine, and
the variable trees returns maple,birch.
RESULT:
The variable trees returns
pine,apple,pear,birch.
› Remove an element. Note: See page 38 for instructions Á Click the button to test ■ Clicking the button
on how to test your movie. your movie. retrieves the current elements
ˇ Move to the test in the array and the deleted
environment. element.
171
3657-5 Ch07.F 1/31/02 12:22 PM Page 172
FLASH ACTIONSCRIPT
Layer 1
Actions Ctrl+Alt+A
⁄ Select the frame, button, Note: This example uses file ‹ Create an array. ■ If you associate the
or movie clip to which you slicearray.fla, which you can find on variables with dynamic text
want to add ActionScript. the CD that accompanies this book. boxes, you can display the
values the variables return to
¤ Click Window ➪ Actions the user.
to open the Actions panel.
172
3657-5 Ch07.F 1/31/02 12:22 PM Page 173
You use the slice method to extract a section of an You use the reverse method to reverse the order of an
existing array and use it to create a new array. If you array. If you have the array shown here.
find it easier to begin your count from the end of the numbers = [100, 200, 300, 400, 500];
array and work backwards, use negative numbers. If
Use the following syntax to reverse the array.
you have the array shown here.
numbers.reverse ( ); 500,400,300,200,100.
trees = ["pine","maple","birch","apple","pear",
"peach","chestnut"]; The variable numbers will return 500, 400, 300, 200, 100.
Use this syntax to extract apple, pear, and peach and
create an array named fruit.
fruit = trees.slice(-4,-1); .
The variable fruit will return: apple,pear,peach.
› Extract a section of the Note: See page 38 for instructions Á Click the button to test ■ Clicking the button
array. on how to test your movie. your movie. displays the original array
and the array created by the
ˇ Move to the test slice.
environment.
173
3657-5 Ch07.F 1/31/02 12:22 PM Page 174
FLASH ACTIONSCRIPT
Layer 1
Actions Ctrl+Alt+A
⁄ Select the frame, button, ¤ Click Window ➪ Actions ‹ Create an array. ■ If you associate the variable
or movie clip to which you to open the Actions panel. with a dynamic text box, you
want to add ActionScript. can display the value the
variable returns to the user.
Note: This example uses file
joinarray.fla, which you can find on
the CD that accompanies this book.
174
3657-5 Ch07.F 1/31/02 12:22 PM Page 175
Example
nameAge = ["George", 23, false,"Jane", 45, true];
RESULT:
George,23,false,Jane,45,true.
› Convert the array to a Note: See page 38 for instructions ˇ Click the button to test ■ Clicking the button
string. on how to test your movie. your movie. displays the original array
and the new string.
■ Move to the test
environment.
175
3657-5 Ch08.F 1/31/02 12:23 PM Page 176
FLASH ACTIONSCRIPT
CHANGE COLORS
ou can use Color object methods to change the color Use the objectName argument to specify the Color object.
Actions Ctrl+Alt+A
⁄ Select the frame, button, ¤ Click Window ➪ Actions ‹ Create an instance of the › Set the color using
or movie clip to which you to open the Actions panel. Color object using the new setRGB.
want to add ActionScript. Color constructor.
Note: This example uses file color.fla,
which you can find on the CD that
accompanies this book.
176
3657-5 Ch08.F 1/31/02 12:23 PM Page 177
you count
1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 13 14 15 16 17 18 19 1a 1b
1c 1d 1e 1f 20 21 22 23 24
The getRGB method returns the color value in base 10. You can use the
Number.toString method to convert a base 10 number to base 16.
The syntax for the Number.toString method is
number.toString(radix) .
Use the number argument to specify the number you want to convert to
a string. Use the radix argument to specify the numeric base. You can
specify any base from 2 to 36. If you do not specify a base, ActionScript
uses base 10. This example converts the results of a getRGB call to base
16 or hexadecimal: (newColor.getRGB()).toString(16); .
ˇ Get the hexadecimal code ■ If you associate the Á Move to the test ‡ Click the button to test
using getRGB. variable with a dynamic text environment. your movie.
box, you can display the
value the variable returns to Note: See page 38 for instruction on ■ Clicking the button
the user. how to test your movie. changes the color of the
movie clip and displays the
hexadecimal code.
177
3657-5 Ch08.F 1/31/02 12:24 PM Page 178
FLASH ACTIONSCRIPT
adjust the percentage of red, green, blue, or alpha from You use the Color.setTransform method to set the
+100 to –100 percent. You can also adjust the red, green, color transform. The syntax for the Color.setTransform
blue, or alpha offset values by an amount ranging from –255 method is
to +255. Flash combines the adjustments you make to
produce a new color. objectName.setTransform(transformObject); .
The parameters for making color adjustments are ra, rb, Use the objectName argument to specify the
ga, gb, ba, bb, aa, and ab. Parameters beginning with r variable to which you stored the Color object. Use
adjust the red value, parameters beginning with g adjust the the transformObject argument to specify the variable
green value, parameters beginning with b adjust the blue to which you assigned the new object.
value, and parameters beginning with a adjust the alpha You use the Color.getTransform() method to retrieve
value. Parameters ending with a adjust the percentage. the transform values set by the most recent setTransform
Parameters ending with b adjust the offset value. call. The syntax for the Color.getTransform() method is
Setting a color transform is a four-step process: 1) Use the objectName.getTransform().
constructor new Color() to create an instance of the Color
object. Refer to Chapter 7 for more information on creating Use the objectName argument to specify the variable to
objects. 2) Create a generic object. Refer to Chapter 7 for which you stored the Color object.
Actions Ctrl+Alt+A
⁄ Select the frame, button, ¤ Click Window ➪ Actions ‹ Create a new Color object › Create a new generic
or movie clip to which you to open the Actions panel. using the new Color object using new Object.
want to add ActionScript. constructor.
Note: This example uses file
colortransform.fla, which you can find
on the CD-ROM that accompanies this
book.
178
3657-5 Ch08.F 1/31/02 12:24 PM Page 179
ˇ Assign color.setTransform ‡ Move to the test ° Click the button to test ■ Clicking the button applies
values to the properties. environment. your movie. the color transform.
Á Set the color transform. Note: See page 38 for instruction on
how to test your movie.
179
3657-5 Ch08.F 1/31/02 12:24 PM Page 180
FLASH ACTIONSCRIPT
Actions Ctrl+Alt+A
⁄ Select the frame, button, ¤ Click Window ➪ Actions ‹ Set the clip event to ■ This will cause the script
or movie clip to which you to open the Actions panel. mouseMove. to execute each time the
want to add ActionScript. mouse moves.
Note: This example uses file
mouse.fla, which you can find on the
CD that accompanies this book.
180
3657-5 Ch08.F 1/31/02 12:24 PM Page 181
› Assign _xmouse and ˇ Move to the test Á Drag your mouse to test ■ As you drag your mouse,
_ymouse to variables. environment. your movie. you will see the _xmouse
and _ymouse values change.
■ If you associate the Note: See page 38 for instruction on
variables with dynamic text how to test your movie.
boxes, you can display the
values the variables return to
the user.
181
3657-5 Ch08.F 1/31/02 12:24 PM Page 182
FLASH ACTIONSCRIPT
Before calling date methods, you must use the new Date Use the getDate() method to retrieve the day of the
constructor to create an instance of the Date object. Use month based on local time. Use the getUTCDate()
the syntax objectName = new Date(); . Use the method to retrieve the current day based on Universal
objectName argument to name the Date object. Coordinated Time. The getDate() and the
getUTCDate() methods return an integer from 1 to 31,
ActionScript provides several methods you can use representing the day of the month.
to retrieve date and time values using the syntax
objectName.method(); . Use the objectName argument Use the getFullYear() method to retrieve a four-digit
to specify the name you assigned to the Date object. Use the number representing the year based on local time. Use the
method argument to specify the method you want to use. getUTCFullYear() method to retrieve a four-digit
number representing the year based on Universal
Some date and time methods retrieve the local date and Coordinated Time.
time. Other date and time methods retrieve the date and
time based on Greenwich Mean Time — also referred to as
Universal Coordinated Time.
Actions Ctrl+Alt+A
⁄ Select the frame, button, ¤ Click Window ➪ Actions ‹ Set the event to ■ This causes the script to
or movie clip to which you to open the Actions panel. enterFrame. execute each time the cursor
want to add ActionScript. enters the frame.
Note: This example uses file date.fla,
which you can find on the CD that
accompanies this book.
182
3657-5 Ch08.F 1/31/02 12:24 PM Page 183
RESULT:
The getFullYear() and getUTCFullYear()
The new Date constructor returns the current methods return a four-digit year. They return 2002
date in the format shown here.: to represent the year 2002. You can use the
Sun Oct 7 08:39:05 GMT-0400 2002. getYear() method to retrieve the year based on
local time. The getYear() method returns the full
year minus 1900. The getYear() method returns
102 to represent the year 2002.
› Create an instance of the Á Concatenate the values ‡ Move to the test ° Test your movie.
Date object. and assign them to a variable. environment.
■ When you enter the test
ˇ Get the current month, ■ This puts the date in a Note: See page 38 for instruction on environment, the screen will
date, and full year. standard date format. If you how to test your movie. automatically display the
associate the variable with a current date.
■ Add +1 to the month value dynamic text box, you can
because the month value display the value the variable
returns 0 for January and 1 returns to the user.
for February and so forth. 183
3657-5 Ch08.F 1/31/02 12:24 PM Page 184
FLASH ACTIONSCRIPT
Use the getDay() method to retrieve the day of the week Use the getSeconds() method to retrieve the number of
based on local time. Use the getUTCDay() method to seconds that have elapsed in the current minute based on
retrieve the day of the week based on Universal local time. Use the getUTCSeconds() method to retrieve
Coordinated Time. The getDay() and getUTCDay() the number of seconds that have elapsed in the current
methods return 0 to represent Sunday, 1 to represent minute based on Universal Coordinated Time. The
Monday, and so forth. getSeconds() and the getUTCSeconds() methods
return an integer from 0 to 59.
Use the getHours() method to retrieve the hour of the
day based on local time. Use the getUTCHours() method Use the getMilliseconds() method to retrieve the
to retrieve the hour of the day based on Universal number of milliseconds that have elapsed in the current
Coordinated Time. The getHours() and getUTCHours() second based on local time. Use the
methods return an integer from 0 to 23. The value 0 getUTCMilliseconds() method to retrieve the number
represents midnight and the value 23 represents 11 p.m. of milliseconds that have elapsed in the current second
based on Universal Coordinated Time. The
Use the getMinutes() method to retrieve the number of getMilliseconds() and getUTCMilliseconds()
minutes that have elapsed in the current hour based on methods return an integer from 0 to 999.
local time. Use the getUTCMinutes() method to retrieve
Actions Ctrl+Alt+A
⁄ Select the frame, button, ¤ Click Windows ➪ Actions ‹ Set the event to ■ This causes the script to
or movie clip to which you to open the Actions panel. enterFrame. execute each time the cursor
want to add ActionScript. enters the frame.
Note: This example uses file clock.fla,
which you can find on the CD that
accompanies this book.
184
3657-5 Ch08.F 1/31/02 12:24 PM Page 185
A Web page user can be in any time zone. When Use the date.getTime method to retrieve the
displaying information that is time specific, you number of milliseconds since midnight January 1,
can display the information in universal time and 1970, universal time. This method is useful when
provide the user with the difference between you need to compare an instant in time across two
universal time and local time. Use the or more time zones. You may want to use this
getTimezoneOffset method to retrieve method if you create a game for players in different
the difference in minutes between local time zones. Using this method, you can calculate
time and Universal Coordinated Time. The the elapsed time for each player using the same
getTimezoneOffset method does not base.
need to make adjustments for daylight savings
time because the computer system clock makes
the adjustments.
› Create an instance of the ‡ Assign concatenated ° Move to the test ■ When you enter the test
Date object. values to a variable. environment. environment, the screen will
automatically display the
ˇ Assign the date values to ■ If you associate the Note: See page 38 for instruction on time.
variables. variable with a dynamic text how to test your movie.
box, you can display the
Á Concatenate the values. value the variable returns to · Test your movie.
■ This gives the values the the user.
appearance of a clock. 185
3657-5 Ch08.F 1/31/02 12:24 PM Page 186
FLASH ACTIONSCRIPT
Actions Ctrl+Alt+A
⁄ Select the frame, button, ¤ Click Window ➪ Actions ‹ Create an instance of the ■ If you associate the
or movie clip to which you to open the Actions panel. Date object. variable with a dynamic text
want to add ActionScript. box, you can display the
value the variable returns to
Note: This example uses file date.fla, the user.
which you can find on the CD that
accompanies this book.
186
3657-5 Ch08.F 1/31/02 12:24 PM Page 187
› Get the milliseconds. ■ If you associate the ˇ Move to the test Á Click the button to test
variable with a dynamic text environment. your movie.
■ By default, the Date object box, you can display the
does not display value the variable returns to Note: See page 38 for instruction on ■ The screen displays the
milliseconds. the user. how to test your movie. Date object.
187
3657-5 Ch08.F 1/31/02 12:24 PM Page 188
FLASH ACTIONSCRIPT
Actions Ctrl+Alt+A
⁄ Select the frame, button, ¤ Click Window ➪ Actions ‹ Create instances of the ■ If you associate the
or movie clip to which you to open the Actions panel. Date object. variables with a dynamic text
want to add ActionScript. box, your can display the
value the variables return to
Note: This example uses file the user.
setdate.fla, which you can find on the
CD that accompanies this book.
188
3657-5 Ch08.F 1/31/02 12:24 PM Page 189
You can update the date. For example, if you set You can use the setYear method to
the date to January 18, 2003 and you later want set a four-digit year in local time. The
to set the date to January 12, 2003, you can use syntax for the setYear method is
the setDate method to change the day without objectName.setYear(year); .
affecting the month and the year. Use the year argument to specify a
four-digit year.
› Change the date values. ˇ Move to the test Á Click the button to test ■ The screen displays the
environment. your movie. changes you made to the
Date objects.
Note: See page 38 for instruction on
how to test your movie.
189
3657-5 Ch08.F 1/31/02 12:24 PM Page 190
FLASH ACTIONSCRIPT
Actions Ctrl+Alt+A
⁄ Select the frame, button, ¤ Click Window ➪ Actions ‹ Create instances of the › Change the time values.
or movie clip to which you to open the Actions panel. Date object.
want to add ActionScript. ■ If you associate the
variables with dynamic text
Note: This example uses file boxes, you can display the
settime.fla, which you can find on the values the variables return to
CD that accompanies this book. the user.
190
3657-5 Ch08.F 1/31/02 12:24 PM Page 191
Use the Date.UTC method to retrieve the number of milliseconds When displaying dates in text
between January 1, 1970, universal time and the date and time boxes, make the box long
specified in the Date.UTC arguments. The syntax for the enough to display the entire
Date.UTC method is Date.UTC(year,month,date,hour, date. If Flash cannot display a
minute,second,millisecond); . date element in its entirety, it
truncates.
Use the year argument to specify a four-digit year. Use the month
argument to indicate the month. Use 0 to represent January, 1 to
represent February, and so forth. The month argument is optional.
Use the date argument to specify the day of the month. Use an
integer from 1 to 31. The date argument is optional. Use the hour
argument to specify the hour. Use an integer from 0 to 23. Use 0 to
represent midnight; use 23 to represent 11 p.m. The hour argument
is optional. Use the minute argument to specify the minute. Use
an integer between 0 and 59. The minute argument is optional.
Use the second argument to specify the second. Again, use an
integer between 0 and 59. The second argument is also optional.
ˇ Get the milliseconds. Á Move to the test ‡ Click the button to test ■ The screen displays the
environment. your movie. changes you made to the
Date objects.
Note: See page 38 for instruction on
how to test your movie.
191
3657-5 Ch08.F 1/31/02 12:24 PM Page 192
FLASH ACTIONSCRIPT
⁄ Select the frame, button, ‹ Convert degrees to ˇ Move to the test ‡ Click the button to test
or movie clip to which you radians. environment. your movie.
want to add ActionScript.
› Calculate the sine. Note: See page 38 for instruction on ■ The screen displays the
Note: This example uses file trig.fla, how to test your movie. results of the calculation.
which you can find on the CD that ■ If you associate the
accompanies this book. variable with a dynamic text Á Type a number.
box, you can display the
¤ Click to open the value the variable returns to
Actions panel. the user.
192
3657-5 Ch08.F 1/31/02 12:24 PM Page 193
W
You can raise a number to a power using the
you may need to raise a number to a script that follows. The variables inputV and
power using the Math.pow method. The inputpowV are input text boxes in which the
syntax for the Math.pow method is Math.
user types the number and the power to which
pow(x, y); .
to raise the number. The variable result is a
Use the x argument to specify the number you dynamic text box. It displays the results of the
want to raise to a power. Use the y argument to calculation on the screen. You attach this script
specify the power to which you want to raise the to a button.
number. The statement Math.pow(3,2); raises 3
to the second power. The statement returns 9. TYPE THIS:
If you want to find the square root of a number, on (release) {
use the Math.sqrt method. The syntax for the result = Math.pow ( inputV, inputpowV);
}
Math.sqrt method is Math.sqrt(x); .
⁄ Select the frame, button, ‹ Find the square root. › Move to the test Á Click the button to test
or movie clip to which you environment. your movie.
want to add ActionScript. ■ Associating the variable
results with a dynamic text Note: See page 38 for instruction on ■ The screen displays the
Note: This example uses file sqr.fla, box enables you to display how to test your movie. square root.
which you can find on the CD that the value the variable returns
accompanies this book. to the user. ˇ Type a value.
FLASH ACTIONSCRIPT
ROUND NUMBERS
ou can use the Math.round method to round a The Math.round, Math.ceil, and Math.floor methods
⁄ Select the frame, button, ‹ Type the formula for ˇ Move to the test ‡ Click the button to test
or movie clip to which you rounding a number. environment. your movie.
want to add ActionScript.
› Assign the formula to a Note: See page 38 for instruction on ■ The screen displays the
Note: This example uses file round.fla, variable. how to test your movie. number rounded to two
which you can find on the CD that digits.
accompanies this book. ■ Associating the variable Á Type a number with more
with a dynamic text box than 2 digits after the decimal
¤ Click to open the enables you to display the point.
Actions panel. value the variable returns to
the user.
194
3657-5 Ch08.F 1/31/02 12:24 PM Page 195
Y
The random function provides you with an
purposes, such as displaying a movie clip at random alternate way to generate a random number.
locations on the Stage. You can use the Math.random The random function returns integers between 0
method to generate random numbers. The syntax for the and a specified value. The syntax for the random
Math.random method is Math.random(); . The
function is random(value); .
Math.random method returns a random number between
0 and 1. If you want to generate a number between 0 and Use the value argument to set the highest
another value, multiply Math.random by the value you value the random function should return. The
want to set as the limit. For example, if you want to random function will return any value between
generate a random number between 0 and 10, multiply
0 and the value you specify minus 1.
Math.random by 10.
TYPE THIS: RESULT:
The number generated might not be a whole number. You
can use the Math.round method to round the number to Random(5); 0, 1, 2, 3, or 4
an integer. This example returns random integers between
1 and 10: Math.round(Math.random() * 10); .
Flash 5 deprecated the random function. If
You might want to generate random numbers between two you are authoring for a Flash 5 environment,
numbers. This example generates integers between 50 and use Math.random instead.
60: 50 + Math.round(Math.random()*10); .
⁄ Select the frame, button, ‹ Type the formula for ˇ Move to the test ■ The screen displays a
or movie clip to which you generating a random number environment. random number between 1
want to add ActionScript. between 1 and 10. and 10 each time you click
Note: See page 38 for instruction on the button.
Note: This example uses file › Assign the result to a how to test your movie.
random.fla, which you can find on the variable.
CD that accompanies this book. Á Click the button to test
your movie.
¤ Click to open the
Actions panel.
195
3657-5 Ch08.F 1/31/02 12:24 PM Page 196
FLASH ACTIONSCRIPT
Use the x and y arguments to specify the values or expressions Use the x argument to specify the number or expression
you want to compare. For example, Math.max(23, 4); whose absolute value you want to find. The statement
compares 23 and 4 and returns 23; Math.min(23, 4); Math.abs(–15) returns 15.
compares 23 and 4 and returns 4. If you send two equal
values to Math.max and Math.min, the method returns the You can use the Math.abs method with integers or
value sent. You cannot use Math.max and Math.min to floating-point numbers. If you assign a non-numeric value
compare non-numeric characters. If you send a non-numeric to Math.abs, Math.abs will return NaN.
value to Math.max or Math.min, the method will return
NaN, which means not a number.
Actions Ctrl+Alt+A
⁄ Select the frame, button, ¤ Click Window ➪ Actions ‹ Determine the maximum ■ Associating the variables
or movie clip to which you to open the Actions panel. value. highNo and lowNo with
want to add ActionScript. dynamic text boxes enables
› Determine the minimum you to display the value the
Note: This example uses file value. variable returns to the user.
maxmin.fla, which you can find on the
CD that accompanies this book. ■ Associating the variables x
and y with input text boxes
allows you to store the values
196 the user enters.
3657-5 Ch08.F 1/31/02 12:24 PM Page 197
Example:
on (release) {
if (x != y) {
highNo = Math.max ( x, y );
lowNo = Math.min ( x, y );
result = highNo + " > " + lowNo;
} else {
result = "x = y";
}
}
ˇ Determine the absolute Á Move to the test ‡ Type numbers in the text ° Click the button to test
values. environment. boxes. your movie.
■ Associating the variables Note: See page 38 for instruction on ■ The minimum, maximum,
ax and ay with dynamic text how to test your movie. and absolute values display.
boxes allows you to display
the value the variable returns
to the user.
197
3657-5 Ch08.F 1/31/02 12:24 PM Page 198
FLASH ACTIONSCRIPT
T last key pressed. You can use the Key object to create
keyboard controls for Flash movies. The methods of
the Key object are essential if you are programming games.
last key pressed. The syntax for the getCode method is
Key.getCode(); .
Use the Key object to give the user the ability to maneuver
objects around the screen using the keyboard. Use the Key The getCode method does not take any arguments. The
object to give the user the ability to change the size or virtual key code for k is 75. If the user presses k, the
shape of objects using the keyboard. Alternatively, use the getCode method returns 75. Using the getCode method,
Key object to respond to the user based on the key pressed. you can have your movie perform actions based on the key
You do not use a constructor to access the Key object. pressed.
ActionScript assigns a virtual key code to every physical key The first 127 characters of every character set have ASCII
on the keyboard. Chapter 14 provides a list of virtual key values. The getAscii method returns the ASCII value for
codes. Virtual key codes are the same across all platforms the last key pressed. The ASCII value for k is 107. If the user
and languages. You use virtual key codes to ensure that presses k, getAscii returns 107.
your movie key controls are the same across all platforms
and languages.
Actions Ctrl+Alt+A
⁄ Select the frame, button, ¤ Click Window ➪ Actions ‹ Set the event to KeyDown. ■ This causes the script to
or movie clip to which you to open the Actions panel. execute every time the user
want to add ActionScript. presses a key.
Note: This example uses file 1key.fla,
which you can find on the CD that
accompanies this book.
198
3657-5 Ch08.F 1/31/02 12:24 PM Page 199
Example:
onClipEvent (keyDown) {
if (Key.isDown(Key.RIGHT)) {
_x = _x + 10;
}
}
onClipEvent (keyDown) {
if (Key.isDown(Key.LEFT)) {
_x = _x - 10;
}
}
onClipEvent (keyDown) {
if (Key.isDown(Key.UP)) {
_y = _y - 10;
}
}
onClipEvent (keyDown) {
if (Key.isDown(Key.Down)) {
_y = _y + 10;
}
}
› Get the ASCII code. ■ Associating the variables ° Move to the test · Test your movie by
with dynamic text boxes environment. pressing keys on the
ˇ Get the virtual key code. enables you to display the keyboard.
values the variables return to Note: See page 38 for instruction on
Á Evaluate the key pressed. how to test your movie. ■ The screen displays the
the user.
■ The value 76 is the virtual ASCII code, the virtual key
key code for the letter L. ‡ Evaluate to see if the Num code, whether you pressed L,
Lock key is on. and the status of the Num
Lock.
CONTINUED 199
3657-5 Ch08.F 1/31/02 12:24 PM Page 200
FLASH ACTIONSCRIPT
Use the keycode argument to specify the virtual key code The onClip Event (keyDown) and the onClip Event
value assigned to the value or the Key object property (keyUp) events respond each time the user presses a key.
assigned to the key. The properties of the Key object When using the Key object, you may want to associate your
represent the keys most commonly used to control games. script with one of these events.
Actions Ctrl+Alt+A
⁄ Select the frame, button, ¤ Click Window ➪ Actions ‹ Use an if statement to set ■ Associating the variable
or movie clip to which you to open the Actions panel. a condition. with a dynamic text box
want to add ActionScript. enables you to display the
■ If the virtual key code for value the variable returns to
Note: This example uses file 2Key.fla, the key pressed equals the the user.
which you can find on the CD that key code specified, the
accompanies this book. condition is true. ˇ Repeat steps 3 and 4 for
each virtual key code you
› Assign a value to a want to test.
variable if the condition
200 is true.
3657-5 Ch08.F 1/31/02 12:24 PM Page 201
Á Click Control ➪ Test ‡ Type a word to test your ■ The word appears on
Movie to move to the test movie. screen.
environment.
201
3657-5 Ch08.F 1/31/02 12:24 PM Page 202
FLASH ACTIONSCRIPT
Linkage...
SET THE SYMBOL LINKAGE ¤ Select the sound symbol. › Click Linkage to open the ‡ Click OK.
PROPERTY Symbol Linkage Properties
‹ Click to open the dialog box. ° Save your .FLA file.
⁄ Click Window ➪ Library Options menu.
to open the Library. ˇ Click Export this symbol ■ After setting the Symbol
for the Linkage type Linkage Property, you must
Note: This example uses file sound.fla, save your file.
which you can find on the CD that („ changes to ´).
accompanies this book. Á Type a name in the
202 Identifier field.
3657-5 Ch08.F 1/31/02 12:24 PM Page 203
Frame 1
status = false;
Button
on (release) {
if (status == false) {
beat = new Sound();
beat.attachSound("music");
beat.start(0,10);
status = true;
onOff = "Click to Stop";
} else {
beat.stop();
status = false;
onOff = "Click for Music";
}
}
Actions Ctrl+Alt+A
ADD SOUND ¤ Click Window ➪ Actions ‹ Set a variable to false. ■ You are initializing the
to open the Actions panel. variable. Frequently, you
⁄ Select the frame, button,
initialize variables in frame 1.
or movie clip to which you
Subsequent statements reset
want to add ActionScript.
the value. In this case, false
means no sound is playing.
CONTINUED
203
3657-5 Ch08.F 1/31/02 12:24 PM Page 204
FLASH ACTIONSCRIPT
Y syntax for the Sound.start method is you want to stop. Enclose the idName in quotes. The
idName argument is optional. If you do not specify an
idName, all sounds will stop.
soundName.start(secondOffset,loop); .
You can also use the stopAllSounds action to stop the
Use the soundName argument to specify the name of the sounds. Sounds set to streaming will resume playing when
Sound object. Use the secondOffset argument to specify the playhead moves over them. The syntax for the
the point at which you want the sound to start. If you have stopAllSounds action is
a 20-second sound and you specify a secondOffset of 10,
the sound will start playing in the middle of the sound. stopAllSound(); .
Note that this argument does not delay the start of the
sound 10 seconds, but instead starts the sound at the 10- The stopAllSounds action does not take any arguments.
second mark. The secondOffset argument is optional.
Use the loop argument to specify the number of times the In Flash, you can play multiple sounds at the same time. You
sound should loop. The loop argument is also optional. can play background music, while a narrator narrates your
movie. You can start sound in a frame or using any of the
Once you have started your sound, you use the stop button or movie clip handlers. That means you can start
argument to stop it. The syntax for the stop argument is sound with the press of a button, when the user rolls over
an object, or upon entry into a frame.
soundName.stop("idName"); .
204
3657-5 Ch08.F 1/31/02 12:24 PM Page 205
Button
on (release) {
vol = beat.getVolume();
if (vol < 95) {
beat.setVolume(vol + 5);
vol = beat.getVolume();
} else {
beat.setVolume(100);
vol = beat.getVolume();
}
· Reset the value of the — Stop the sound. ¡ Move to the test ™ Click the button to test
variable. environment. your movie.
± Reset the value of the
■ The sound is now playing variable. Note: See page 38 for instruction on ■ Clicking the button toggles
so you set the status to true. how to test your movie. sound on and off.
■ The sound is no longer
‚ Create an else statement. executing so you set the
status to false.
■ If the status is not equal to
false, these statements will
execute. 205
3657-5 Ch08.F 1/31/02 12:24 PM Page 206
FLASH ACTIONSCRIPT
Actions Ctrl+Alt+A
⁄ Select the frame, button, ¤ Click Window ➪ Actions ‹ Create and start a sound. ˇ Get the volume value.
or movie clip to which you to open the Actions panel.
want to add ActionScript. › Get the pan value. ■ Associating the variable
with a dynamic text box
Note: This example uses file ■ Associating the variable enables you to display the
volppan.fla, which you can find on the with a dynamic text box, value the variable returns to
CD that accompanies this book. enables you to display the the user.
value the variable returns to
206 the user.
3657-5 Ch08.F 1/31/02 12:24 PM Page 207
Button
on (release) {
pan = beat.getpan();
if (pan < 95) {
beat.setPan(pan + 5);
pan = beat.getPan();
} else {
beat.setPan(100);
pan = beat.getPan();
}
}
Á Select the frame, button, ° Get the new pan and ‚ Click the button to test ■ Clicking the button resets
or movie clip to which you volume values. your movie. the pan and volume values.
want to add ActionScript.
· Move to the test
‡ Reset the volume and pan environment.
values.
Note: See page 38 for instruction on
how to test your movie.
207
3657-5 Ch08.F 1/31/02 12:24 PM Page 208
FLASH ACTIONSCRIPT
Selection.getEndIndex(); . If you need the variable name of the field in which the
cursor is currently located, use the Selection.getFocus
The Selection.getBeginIndex and the method. The syntax for the Selection.getFocus
Selection.getEndIndex methods do not take method is
arguments. The Selection.getBeginIndex method
returns the index position of the beginning of the selection. Selection.getFocus(); .
The selection.getEndIndex method returns the index
position of the end of the selection. The first position in a The Selection.getFocus method does not take any
text box has an index position of 0, the second an index arguments. It returns the name of the variable in which the
position of 1, and so forth. When the cursor is not located cursor is currently located. If the cursor is not located in the
text box, selection.getFocus returns null.
USING THE SELECTION OBJECT
Actions Ctrl+Alt+A
⁄ Select the frame, button, ¤ Click Window ➪ Actions ‹ Set the event to Mouse ■ This causes the script to
or movie clip to which you to open the Actions panel. move. execute every time the mouse
want to add ActionScript. moves.
Note: This example uses file
selections.fla, which you can find on
the CD that accompanies this book.
208
3657-5 Ch08.F 1/31/02 12:24 PM Page 209
Frame 1
setProperty (codeMC, _visible, false);
MovieClip
onClipEvent (mouseMove) {
_root.begin = Selection.getBeginIndex();
_root.end = Selection.getEndIndex();
if (_root.begin == 0 && _root.end == 9) {
_root.response = "Correct!";
} else if (_root.begin ==10 && _root.end ==19) {
_root.response = "Wrong";
} else {
_root.response = "";
}
}
› Get the BeginIndex ‡ Get the Focus value. · Test your movie by ■ The BeginIndex,
value. clicking and dragging to EndIndex, CaretIndex,
° Move to the test select text. and Focus values display
ˇ Get the EndIndex value. environment. on-screen.
Á Get the CaretIndex Note: See page 38 for instruction on
value. how to test your movie.
209
3657-5 Ch09.F 1/31/02 12:24 PM Page 210
FLASH ACTIONSCRIPT
GET PROPERTIES
ovie clips have a number of properties, including function is
GET PROPERTIES
Actions Ctrl+Alt+A
⁄ Select the frame, button, ■ This example uses a ‹ Get the movie clip ■ You can use the
or movie clip to which you button. property values using the getProperty function to
want to add ActionScript. getProperty function. retrieve a property, or you
¤ Click Window ➪ Actions can precede the property
Note: This example uses file to open the Actions panel. with the movie clip name
getproperties.fla, which you can find followed by a dot.
on the CD that accompanies this book.
210
3657-5 Ch09.F 1/31/02 12:24 PM Page 211
DEMYSTIFYING FUNCTIONS 9
You can use the script that follows to toggle the size
of a movie clip. The script is associated with a button.
TYPE THIS:
on (release) {
if (getProperty (_root.blockMC, _height ) == 50 && getProperty (_root.blockMC, _width) == 50) {
setProperty (_root.blockMC, _height, 100);
setProperty (_root.blockMC, _width, 100);
} else if (getProperty (_root.blockMC, _height ) == 100 && getProperty (_root.blockMC, _width) ==100) {
setProperty (_root.blockMC, _width, 150);
setProperty (_root.blockMC, _height, 150);
} else if (getProperty (_root.blockMC, _height ) == 150 && getProperty (_root.blockMC, _width) ==150) {
setProperty (_root.blockMC, _width, 50);
setProperty (_root.blockMC, _height, 50);
}
}
RESULT:
Each click of the button toggles the size of the movie clip blockMC from 50 pixels x 50 pixels, to 100 pixels
x 100 pixels, to 150 pixels x 150 pixels, back to 50 pixels x 50 pixels.
› Move to the test ■ Clicking the button Á Drag the blueMC over the ■ The screen displays the
environment. displays the blueMC redMC. movie clip properties
properties. including dropTarget.
Note: See page 38 for instructions ‡ Click the button to get the
on how to test your movie. dropTarget property.
ˇ Click the button to test
your movie.
211
3657-5 Ch09.F 1/31/02 12:24 PM Page 212
FLASH ACTIONSCRIPT
The syntax for the getTimer function is getTimer(); . You can use getTimer to specify when a movie
The getTimer function does not take any arguments. clip appears on the screen. The example that
follows uses getTimer to display a movie clip
You use the getVersion function to retrieve the version of on the Stage after 5 seconds have elapsed.
Flash Player the user is using and the platform on which the
user is working. The getVersion function only returns Example:
information if the user is using version 5 of Flash Player or onClipEvent (enterFrame) {
higher. The getVersion function is useful when you want _root.timeV = getTimer ();
to display a different set of instructions to each user,
if (_root.timeV >= 5000 ) {
dependent on their platform, or you want to ensure that
setProperty (_root.stopMC, _visible,
the version of Flash Player the user is using is compatible true);
with your movie. }
}
⁄ Select the frame, button, ¤ Click to open the ˇ Move to the test ■ The screen displays the
or movie clip to which you Actions panel. environment. timer and version.
want to add ActionScript.
‹ Get the timer using the Note: See page 38 for instructions
Note: This example uses file getTimer function. on how to test your movie.
vertimer.fla, which you can find on
the CD that accompanies this book. › Get the version using the
getVersion function.
212
3657-5 Ch09.F 1/31/02 12:24 PM Page 213
DEMYSTIFYING FUNCTIONS 9
USING EVAL
ou use the ActionScript eval function to
USING EVAL
⁄ Select the frame, button, ‹ Increment the value of x Á Move to the test ‡ Click the button to test
or movie clip to which you by one. environment. your movie.
want to add ActionScript.
› Assign a random number Note: See page 38 for instructions ■ Clicking the button
Note: This example uses file eval.fla, to a variable. on how to test your movie. retrieves the value assigned to
which you can find on the CD that the variable and the variable
accompanies this book. ˇ Retrieve the values. name.
¤ Click to open the Actions
panel.
213
3657-5 Ch09.F 1/31/02 12:24 PM Page 214
FLASH ACTIONSCRIPT
convert the string to a number. The syntax for the parseFloat function is
USING THE NUMBER FUNCTION ¤ Click to open the USING THE PARSEFLOAT ■ Associating the variable
Actions panel. FUNCTION with a dynamic text box
⁄ Select the frame, button,
› Select the frame, button, enables you to display the
or movie clip to which you ‹ Convert a string to a value the variable returns to
want to add ActionScript. number using the number or movie clip to which you
want to add ActionScript. the user.
function.
Note: This example uses file
tonumber.fla, which you can find on ˇ Convert a string to a ■ Associating the variable
number using the with an input text box
the CD that accompanies this book.
parseFloat function. enables you to accept user
214 input.
3657-5 Ch09.F 1/31/02 12:24 PM Page 215
DEMYSTIFYING FUNCTIONS 9
You normally count 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, and then you add a new column to
form the number 10. You count in base 10. You work with 10 digits. You can
count in other bases. For example, you can count in base 2. When you count in
base 2, you only have two digits with which to work — 0 and 1. You start a new
column after you use the second digit. The table shown here illustrates:
You can use the parseInt function to enter a number in a base other than 10 and
return a base 10 value. Use the radix argument to specify the base of the number
you are entering.
USING THE PARSEINT FUNCTION ■ Associating the variables ° Move to the test ‚ Click the buttons to test
with a dynamic text box environment. your movie.
Á Select the frame, button,
enables you to display the
or movie clip to which you
value the variable returns to Note: See page 38 for instructions ■ The script applies the
want to add ActionScript. on how to test your movie. number, parseFloat, and
the user.
parseInt functions to your
‡ Convert the string to a · Type values in the text
number using the parseInt ■ Associating the variables entries.
with an input text box boxes.
function.
enables you to accept user
input. 215
3657-5 Ch09.F 1/31/02 12:24 PM Page 216
FLASH ACTIONSCRIPT
⁄ Select the frame, button, ¤ Click to open the › Send an error message to ■ Associating the variables
or movie clip to which you Actions panel. the user if a value is not a with dynamic text boxes
want to add ActionScript. number. enables you to display the
‹ Determine if the values values the variables return to
Note: This example uses file are numbers using the isNAN ˇ Set the results of the the user.
tonumber.fla, which you can find on function. calculation to a blank space
the CD that accompanies this book. if a value is not a number. Á Test for infinity using the
■ Associating the variables isFinite function.
with input text boxes enables
216 you to accept user input.
3657-5 Ch09.F 1/31/02 12:24 PM Page 217
DEMYSTIFYING FUNCTIONS 9
You can test user entries to determine if they are numbers. In the example
that follows, if all entries are not numbers, the script returns an error
message. If all entries are numbers, the script calculates an age based on
the entries made.
Example:
on (release) {
if (isNaN(monthV) || isNaN(yearV) ||isNaN( DayV)) {
errorMessage = “Please enter numbers.”;
result = “ “;
} else {
monthU = monthV-1;
birth = new Date( yearV,MonthU, DayV );
today = new Date();
age = Math.floor ( (((((today-birth)/1000)/60)/60)/24)/365);
result = “You are “ + age + “ years old.”;
errorMessage = “”;
}
}
‡ Send an error message to · Perform the calculations if — Move to the test ¡ Click the button to test
the user if the expression there are no errors. environment. your movie.
returns infinity.
‚ Set the error message to a Note: See page 38 for instructions ■ ActionScript will display
° Set the results of the blank space if there are no on how to test your movie. an error message if your entry
calculation to a blank space errors. in not a number or if the
if the expression returns ± Type the value in the input calculation evaluates to
infinity. text boxes. infinity.
217
3657-5 Ch09.F 1/31/02 12:24 PM Page 218
FLASH ACTIONSCRIPT
W may have more text than will fit in the visible area
of the text box. You can use ActionScript to create
buttons or scroll bars that enable the user to scroll up or
mouse after pressing a button, use the syntax shown here:
on (release) {
down the text box or move to a specified line. text.scroll = 1;
}
ActionScript numbers each line in a text box sequentially.
The first line is line number one. Variables that are To retrieve the scroll value, use the syntax scrollValue =
associated with dynamic or input text boxes have a scroll variableName.scroll; . As the user scrolls through a
and maxscroll property. The scroll property returns the text box, the Flash Player updates the variable
topmost visible line number in the text box. You can set or scrollValue.
retrieve this value. The maxscroll property returns the
topmost visible line in the text box when the last line of text The syntax for the maxscroll property is
is visible. You can retrieve the maxscroll value, but you variableName.maxscroll; . Use the variableName
cannot set the maxscroll value. argument to specify the variable name associated with the
text box. The example shown here displays the last lines of
The syntax for the scroll property is text when the user releases the mouse after pressing a
variableName.scroll = x; . Use the variableName button:
argument to specify the variable name associated with the
text box. Use the x argument to specify the line number on (release) {
you want to assign to the scroll property. For example, if text.scroll = text.maxscroll;
}
⁄ Select the frame, button, ¤ Click to open the › Select the frame, button, ˇ Set the scroll value to its
or movie clip to which you Actions panel. or movie clip to which you current value plus one.
want to add ActionScript. want to add ActionScript.
‹ Assign text to an input or ■ Each time the user releases
Note: This example uses file scroll.fla, dynamic text field. ■ The Actions panel will the mouse after clicking the
which you can find on the CD that become active. button, the text box will scroll
accompanies this book. ■ In this example, text is the down one line.
variable name and the ■ This part of this example
variable is associated with an uses a button.
218 input text box.
3657-5 Ch09.F 1/31/02 12:25 PM Page 219
DEMYSTIFYING FUNCTIONS 9
You can use the script shown here to create a
continuous scroll button. When you press the button,
the text box scrolls until you release the button. Start by
creating a button and attach the script shown here.
Example:
on (press) {
var press = true;
}
on (release) {
var press = false;
Example:
onClipEvent (enterFrame) {
if (press == true) {
_root.text.scroll = _root.text.scroll + 1;
}
}
Á Select the frame, button, ‡ Set the scroll value to its ° Move to the test · Click the buttons to test
or movie clip to which you current value minus one. environment. your movie.
want to add ActionScript.
■ Each time the user releases Note: See page 38 for instructions ■ When you click the down
■ The Actions panel will the mouse after clicking the on how to test your movie. button the text scrolls down.
become active. button, the text box will scroll When you click the up button
up one line. the test scrolls up.
219
3657-5 Ch09.F 1/31/02 12:25 PM Page 220
FLASH ACTIONSCRIPT
predefined functions. For example, the functions The following is an example of a custom function that you
getProperty, getTimer, random, and getVersion are can use to round a floating-point number to two digits and
all predefined by ActionScript. return the value
You can create your own custom functions using the function twoDigit (number) {
function action. The syntax for the function action is return math.round(number * 100)/100;
}
function functionName (a1, a2, ...aN){
statement(s) The name of the function is twoDigit. It takes one
}; argument — number — and it returns the value of the
number rounded to two digits after the decimal point.
Use the functionName argument to name your function.
Use a1 to aN to specify the arguments to pass to the When using Normal mode, you use the evaluate action to
function. Arguments are optional. Use the statement include a custom function to your script. The evaluate
argument to define the operation the function performs. action creates an empty line with a semicolon.
⁄ Select the frame, button, ¤ Click to open the › Select the frame, button, ■ Associating the variable
or movie clip to which you Actions panel. or movie clip to which you with a dynamic text box
want to add ActionScript. want to add ActionScript. enables you to display the
‹ Create a function. value the variable returns to
Note: This example uses file ˇ Use the function you the user.
function.fla, which you can find on the created.
CD that accompanies this book. ■ Associating the variable
with an input text box
enables you to receive input
220 from the user.
3657-5 Ch09.F 1/31/02 12:25 PM Page 221
DEMYSTIFYING FUNCTIONS 9
You can call a function from any Timeline, If you omit arguments during a function call,
including the Timelines of loaded movies. To call ActionScript passes the argument to the function as
a function from another Timelime, precede the undefined. This can cause errors when you export
function with the target path using dot syntax. your movie. If you send extra arguments during a
function call, ActionScript ignores them.
Example:
A function does not have to return a value. For
_root.sampleMC.twoDigit(123.456); example, you can create a function that initializes
It is a good practice to document your functions values.
with comments. Include the input, outputs, and
purpose of the function in your comments.
When creating a function, it is a good idea to use
local variables. The scope of a local variable is
limited to the curly braces that enclose it. Using
local variables in a function prevents other
scripts in the movie from reading the variables.
ActionScript treats arguments passed to a
function as local variables.
Á Click Control ➪ Test ‡ Type a floating point ° Click the button to test
Movie to move to the test number with more than two your movie.
environment. digits after the decimal point.
■ The function rounds the
number you entered to two
digits.
221
3657-5 Ch10.F 1/31/02 12:25 PM Page 222
FLASH ACTIONSCRIPT
Layer 1
Symbol Definition(s)
Movie Explorer Ctrl+Alt+M
⁄ Click Window ➪ Movie ■ Movie Explorer opens, and ¤ Click to view nested
Explorer. the display list appears. Timelines.
222
3657-5 Ch10.F 1/31/02 12:25 PM Page 223
The display list in Movie Explorer shows the When using Debugger, you can view the display list.
hierarchy of the movie. You can use the display When viewing the display list in Debugger, the
list to see a graphical representation of a movie addition and removal of movies and movie clips
and all the objects included in the movie. You display instantly. The display list in Debugger also
will find Movie Explorer particularly useful when presents a hierarchical representation of the movie.
you are examining a movie developed by
someone else. Movie Explorer provides you with
a mechanisn that allows you to easily understand
how a movie is put together.
Circle Circle
■ The path to the selected ■ You can use the Filtering ■ The pop-up menu appears. Note: You can use the menu to
item appears on the buttons to limit the categories navigate Movie Explorer, edit
Status bar. of items Movie Explorer objects, or print the hierarchy.
displays.
■ You can use the Find text
box to search for items. ‹ Click to display the
pop-up menu.
223
3657-5 Ch10.F 1/31/02 12:25 PM Page 224
FLASH ACTIONSCRIPT
fixed. You determine the relative path based on the There are two methods used to specify a target path: dot
Timeline that calls the action. syntax and slash syntax. Dot syntax separates each item in
the path with a dot — for example, _level0.redMC.
ActionScript bases both absolute and relative target paths triangleMC. Slash syntax separates each item in the path
on the movie hierarchy. You can view the movie hierarchy with a slash — for example, _level0/redMC/triangleMC.
using the Movie Explorer display list. An absolute path
address begins with the name of the level the targeted ActionScipt bases a relative path on the relationship
movie clip was loaded into and includes the name of each between the controller Timeline and the target Timeline.
instance until it reaches the target instance. For example, if You can only use a relative path to target objects on the
you place a movie clip with an instance name of redMC on same level. Use the alias _parent to refer to the parent of
the main Timeline, the absolute path to redMC would be the current Timeline. When using slash syntax, you use ..
_level0.redMC. If you add an instance named to move up the hierarchy.
Get Path
Functions
Actions Ctrl+Alt+A
targetPath
224
3657-5 Ch10.F 1/31/02 12:25 PM Page 225
targetpath(instanceName);
› Type your expression. ■ If you associate the variable ˇ Move to the test Á Click the button to test
with a dynamic text box, you environment. your movie.
■ This expression retrieves can display the value the
the target path and assigns it variable returns to the user. Note: See page 38 for instructions ■ The targetPath function
to the variable path. on how to test your movie. retrieves the target path.
225
3657-5 Ch10.F 1/31/02 12:25 PM Page 226
FLASH ACTIONSCRIPT
loadMovie(URL , target/location, variable); Flash organizes movies into a hierarchy called levels. You
use the _level property to specify the level. The syntax for
Use the URL argument to specify the absolute or relative the _level property is _levelN; . Use the N argument to
URL of the SWF file that you want to load. Relative paths specify the level number. The movie loaded at _level0
must be based on the location of the SWF file, and the URL sets the frame rate, background color, and frame size for all
must be in a subdomain of the current movie. When you other movies.
are testing Flash movies, all SWF files must be stored in the
same folder.
Basic Actions
Layer 1
⁄ Select the frame, button, ¤ Click Window ➪ Actions ‹ Click ➪ Basic Actions ➪
or movie clip to which you to open the Actions panel. Load Movie to select the
want to add ActionScript. loadMovie Action.
Note: This example uses file load.fla,
which you can find on the CD that
accompanies this book.
226
3657-5 Ch10.F 1/31/02 12:25 PM Page 227
The loadMovie action enables you to break a You can use this script to load a movie and send
large project down into its component parts and variables using the post method.
load each movie when needed. Several small
movies play faster and use memory more Example:
efficiently than one large movie. loadMovieNum ("sample.swf", 1, "POST");
Example:
on (release) {
loadMovie ("redstar.swf", "blueStarMC");
}
› Type the URL of the ■ If you are replacing a ° Move to the test · Click the button to load
movie that you want to load. movie clip, type the target environment. your movie.
path instead of performing
ˇ Click and select the step 6. Note: See page 38 for instructions ■ Your movie loads.
location where the movie on how to test your movie.
will load. ‡ Click and select how
you want to send variables.
■ To load a movie, select
Level. To load a movie clip, ■ You can choose from Don’t
select Target. Send, Send Using Get, or
Send Using Post.
Á Type a level number. CONTINUED 227
3657-5 Ch10.F 1/31/02 12:25 PM Page 228
FLASH ACTIONSCRIPT
Basic Actions
Actions Ctrl+Alt+A
Unload Movie Esc+um
⁄ Select the frame, button, ■ This example uses a button. ‹ Click ➪ Basic Actions ➪
or movie clip to which you Unload Movie to select the
want to add ActionScript. ¤ Click Window ➪ Actions unloadMovie Action.
to open the Actions panel.
Note: This example uses file load.fla,
which you can find on the CD that
accompanies this book.
228
3657-5 Ch10.F 1/31/02 12:25 PM Page 229
The following example loads a movie. When you load a movie on another movie, the
loaded move is aligned with the upper left corner of
Example: the movie into which it is loaded. To ensure proper
loadMovieNum ("sample.swf", 1); alignment, it is a good idea to make both movies
The following example unloads a previously the same size.
loaded movie.
Example:
unloadMovieNum (1);
Example:
loadMovie ("sample.swf", "sampleMC");
› Type a Level number. ˇ Move to the test Á Click the button to load ‡ Click the button to unload
environment. your movie. your movie
Note: See page 38 for instructions ■ Your movie loads. ■ Your movie unloads.
on how to test your movie.
229
3657-5 Ch10.F 1/31/02 12:25 PM Page 230
FLASH ACTIONSCRIPT
Basic Actions
Start Movie
Actions Ctrl+Alt+A
⁄ Select the frame, button, ¤ Click Window ➪ Actions ‹ Click ➪ Basic Actions ➪ ■ If you want to open the
or movie clip to which you to open the Actions panel. Tell Target to select the Target Path dialog box, click
want to add ActionScript. tellTarget Action. the Insert Target Path
dialog box.
Note: This example uses file
telltarget.fla, which you can find on
the CD that accompanies this book.
230
3657-5 Ch10.F 1/31/02 12:25 PM Page 231
Before you can tartget a a Timeline, the Timeline Flash 5 deprecated the tellTarget action. You
must be in Flash Player. You cannot target the should use the with action when you are authoring
Timeline of a movie or movie clip that is not for a Flash 5 environment.
loaded. If you are targeting a movie clip, the
playhead must be located in one of the frames of
the movie clip. If the movie clip is in frames 1 to
10 and the playhead is in frame 11, you cannot
target the movie clip.
› Type a Level number. Á Move to the test ‡ Click the button to load ° Click the button to start
environment. your movie. your movie
ˇ Type your statements.
Note: See page 38 for instructions ■ Your movie will load. ■ Your movie will start.
■ Type the actions you want
on how to test your movie.
to execute.
231
3657-5 Ch10.F 1/31/02 12:25 PM Page 232
FLASH ACTIONSCRIPT
Actions
Start Movie
Actions Ctrl+Alt+A
with Esc+wt
Example:
on (release) {
tellTarget ("block") {
_yscale = 75;
_xscale = 50;
_alpha = 40;
}
}
› Type a level number if ˇ Type your statement(s). Á Move to the test ■ The movie will load.
you are targeting a movie environment
loaded with the loadMovie ■ Type the statements you ° Click the button to start
action or type a target path if want to execute. Note: See page 38 for instructions your movie
on how to test your movie.
you are targeting a movie ■ The movie will start.
clip. ‡ Click the button to load
your movie.
233
3657-5 Ch10.F 1/31/02 12:25 PM Page 234
FLASH ACTIONSCRIPT
playing. The syntax for the duplicateMovieClip action is ActionScript places the child movie clip directly on top of
the parent. You can use the _x and/or _y properties to
duplicateMovieClip(target, newName, depth); change the location of the duplicated movie clip. Each
instance of the duplicated movie clip must have a unique
Use the target argument to specify the name of the name. You can append an incrementing number to the end
instance that you want to duplicate. Use the newName of the movie clip name to make each instance unique.
argument to specify the name that you want to give to the
new instance. The depth argument is used to specify You use the removeMovieClip action to remove instances
stacking order. Stacking order determines how objects created with the duplicateMovieClip action. The syntax
appear on the Stage when objects overlap. Objects with a for the removeMovieClip action is
higher depth number appear to be in front of objects with a
lower number. Assign each object that you duplicate a removeMovieClip(target);
depth number. If you place a new instance on the same
depth level as an existing instance, the new instance Use the target argument to specify the movie clip that
replaces the existing instance. you want to remove. You can also use the
removeMovieClip action to remove movie clips created
Duplicated movie clips always begin playing in frame 1. with the attachMovie and duplicateMovie methods.
⁄ Click to open the Note: This example uses file ‹ Duplicate the movie clip. ■ Each time the user clicks
Action panel. duplicate.fla, which you can find on the Duplicate button, the
the CD that accompanies this book.
› Reset the _x property for new duplicate instance
¤ Select the frame, button, the new instance so that the replaces any existing
or movie clip to which you ■ This part of this example new instance does not appear duplicate instance.
want to add ActionScript. uses a button. on top of the existing
instance. ˇ Select the frame, button,
or movie clip to which you
want to add ActionScript.
234
3657-5 Ch10.F 1/31/02 12:25 PM Page 235
Á Remove the movie clip. Note: See page 38 for instructions ° Click the button to · Click the button to
on how to test your movie. duplicate your movie clip. remove the duplicate movie
‡ Move to the test clip.
environment. ■ A duplicate of your movie
clip will be created. ■ The duplicate of your
movie clip will be removed.
235
3657-5 Ch11.F 1/31/02 12:26 PM Page 236
FLASH ACTIONSCRIPT
A
remove parameters and values. You use the and
reprogram without using the Action panel. Smart buttons to change the order of parameters.
clips enable you to create ActionScript that people
with no programming ability can modify. You define a list The Define Clip Parameters dialog box enables you to add
of options that affect the movie clip look or behavior. the values from which the designer can choose. You specify
Designers select from those options. a value type of Default, Array ,List, or Object. Default
enables you to default the value the designer can select to a
You start the creation of a smart clip by creating a movie string or number. Array enables you to provide the designer
clip. Include any values you want the smart clip to pass to with a list of values from which he can choose. The designer
the movie clip as variables in the movie clip. can add or remove values from the list. List enables you to
create a list of values that the designer can choose from but
You define smart clips in the Define Clip Parameters dialog cannot modify. The Object type enables you to declare
box, which you can find by selecting Define Clip Parameters several related elements and specify the names and values.
from the Options menu of the Library. In the Define Clip For example, you can use the Object type to specify the
Parameters dialog box, you use the button to add elements in an array.
new parameters and values. You use the button to
Background
Library Ctrl+L
⁄ Create a movie clip. ¤ Include variables that the ‹ Click Window ➪ Library › Click to select the movie
user can set in your script. to open the Library panel. clip in the Library.
Note: This example uses file
smartclip.fla, which you can find on Note: A parameter is a variable.
the CD that accompanies this book.
236
3657-5 Ch11.F 1/31/02 12:26 PM Page 237
When you place a movie clip in the Library, it is You use smart clip parameters to change the
available only to that movie. You can use the behavior or appearance of a movie clip. Smart clip
common library to make a movie clip available parameters are passed to the movie clip when the
to all movies. Because smart clips are reusable movie clip loads. Smart clips display a unique icon
code, it is a good idea to place them in the in the Library window.
common library.
To add a file to the common library, copy
the file to the Libraries folder under the
Flash application folder. To use the file, click
Window ➪ Common Libraries and select the
file from the submenu. Drag the file from
the common library onto the Stage.
CONTINUED
237
3657-5 Ch11.F 1/31/02 12:26 PM Page 238
FLASH ACTIONSCRIPT
A smart clip parameter is a variable. When you create or You start by creating ActionScript that will change the
modify your movie, you define the variables. The designer background color, including a variable that defines the
uses the Clip Parameters panel to select parameter values. color. Enter the name of the variable that defines the color
Selecting a parameter value assigns a value to the variable. in the Name field of the Define Clip Parameters box. Use
the Value field to list the possible values.
You can use smart clips for any repetitive programming
tasks. For example, you can create menus, buttons, list When the designer opens the Clip Parameters panel, he
boxes, logos, games, or other types of movie clips and use will be able to select the hexadecimal code he wants to
smart clips to change or modify them. use from the list you provide.
Background Panels
0x000000
Clip Parameters
‚ Click to add a value. ± Click OK. ™ Select the movie clip. £ Click Window ➪ Panels ➪
Clip Parameters to open the
— Double-click here and ¡ Click OK. ■ Select the black Clip Parameters panel.
type a value. background.
238
3657-5 Ch11.F 1/31/02 12:26 PM Page 239
A smart clip custom interface enables you to pass It is a good practice to save the custom interface
values to a movie clip using a movie instead of SWF file and the FLA for the smart clip that uses it
the Clip Parameters panel. If you create a custom in the same directory. If you use your smart clip in
interface, when the designer selects Window ➪ multiple files, the SWF file for the custom interface
Panels ➪ Clip Parameters from the menu, Flash and the FLA for the smart clip should always be in
presents the custom interface instead on the Clip the same relative location.
Parameters panel. The designer makes selections
If you place your custom smart clip in the common
from the custom interface.
library, you must copy the SWF to the Libraries
folder with the same relative path that you specified
in the Define Clip Parameters dialog box.
Background
0xff9966
¢ Click to select a value. ∞ Move to the test § Your movie plays ■ The background of the
environment. automatically when you movie appears in the color
■ You can use your smart enter the test environment. you selected in the Clip
clip to select a background Note: See page 38 for instructions Parameters panel.
color for your movie. on how to test your movie.
239
3657-5 Ch11.F 1/31/02 12:26 PM Page 240
FLASH ACTIONSCRIPT
custom interface for a smart clip. A custom interface Intermediary movie clips have an instance name of xch.
enables you to pass values to a movie clip using a movie You must place an instance of the exchange movie clip on
instead of the Clip Parameters panel. Using a custom the main Timeline of the custom interface movie. Create a
interface, you can create smart clips that are user friendly layer and name the layer Exchange Clip. See Chapter 1 for
and aesthetically pleasing. more information on creating layers. Place the intermediary
movie clip in frame 1 of the Exchange Clip layer. The
You start by creating a movie that will act as the interface. intermediary movie clip appears on the Stage as a small
The movie should provide the user with options that can be dot. Use the Instance panel to name the movie clip instance
used to select the values the user wants to set. xch. The xch movie clip instance must always be loaded.
You can pass arrays and objects through the xch movie clip,
You pass the values to the smart clip using an intermediary, but you cannot pass nested arrays or objects.
or exchange, movie clip. An intermediary movie clip
contains nothing. It is completely blank. You do not place
any graphics, buttons, movie clips, or ActionScript in an
intermediary movie clip.
⁄ Create a movie to use as a Note: This example uses file ■ You must create the ‹ Click Insert ➪ New
custom interface. colorsc.fla, which you can find on intermediary movie clip Symbol to open the Symbol
the CD that accompanies this book. with an instance name of Properties dialog box.
xch. Do not place any
¤ Add statements that pass graphics, buttons, movie › Type a name.
parameters to the xch movie
clip.
clips, or ActionScript in the ˇ Click Movie Clip to
intermediary movie clip. select it („ changes to ´).
Á Click OK.
240
3657-5 Ch11.F 1/31/02 12:26 PM Page 241
Library Ctrl+L
‡ Click to create a new ° Double-click and type · Click Window ➪ Library — Drag the intermediary
layer. to name the layer Exchange to open the Library. movie clip from the Library
Clip. onto the Stage.
‚ Click to select frame 1 of
Note: See page 4 for more the Exchange Clip layer. ± Select the instance that
information on layers. you just dragged onto the
Stage.
CONTINUED
241
3657-5 Ch11.F 1/31/02 12:26 PM Page 242
FLASH ACTIONSCRIPT
Exchange Clip
Panels
Library Ctrl+L
Instance Ctrl+I
¡ Click Window ➪ Panels ➪ £ Publish your movie as an ¢ Open the movie with ∞ Click Window ➪ Library
Instance to open the Instance SWF file. which you want to associate to open the Library.
panel. the smart clip.
Note: See page 32 for information on § Click to select the movie
™ Type xch to name the how to publish your movie. Note: This example uses with which you want to
instance. smartclip2.fla, which you can find associate the custom movie
on the CD that accompanies this clip.
book. ¶ Click Options.
242
3657-5 Ch11.F 1/31/02 12:26 PM Page 243
Learning Interactions includes a feature called To include Learning Interactions Library smart
Knowledge Track. Knowledge Track allows you to clips in your movie, select a keyframe. Then
send data to a learning management system. You click Window ➪ Common Libraries ➪ Learning
can use Lotus LearningSpace, or another tracking Interactions and drag a Learning Interactions
system. Knowledge Track uses AICC (Aviation smart clip onto the Stage.
Industry CBT Committee) specification version
Then click Window ➪ Panels ➪ Clip Parameters
2.0, an industry standard protocol for
to open the clip parameters panel. If you select
courseware-to-tracking system communications.
TrueFalse, you can use the clip parameters panel to
You can set the following values: interactionID,
define your question, select the correct answer, and
ObjectiveID, and Weighting.
specify the feedback you want to send to the user. A
navigation tab enables you to specify how you want
Flash to navigate.
• Click Define Clip ª Associate the smart clip – Select the movie clip with ‘ Click a color to select an
Parameters to open the with the movie. which you associated the option.
Define Clip Parameters custom smart clip.
dialog box. º Click OK. ■ When the Clip Parameters
≠ Click Window ➪ Panels ➪ panel opens, you can select
Clip Parameter to open the options using the custom
Clip Parameters dialog box. smart clip.
243
3657-5 Ch12.F 1/31/02 12:26 PM Page 244
FLASH ACTIONSCRIPT
VALIDATE A STRING
n important part of collecting information from the The product code in the preceding example must be
VALIDATE A STRING
Actions Ctrl+Alt+A
⁄ Select the frame, button, ¤ Click Window ➪ Actions ‹ Use the length function ■ This makes the input check
or movie clip to which you to open the Actions panel. to determine if the user has insensitive to case.
want to add ActionScript. made an entry.
ˇ Check the length of the
Note: This example uses file › Check the value of a input.
valstring.fla, which you can find on the substring, convert the input
CD-ROM that accompanies this book. to lowercase, and compare it
to a lowercase value.
244
3657-5 Ch12.F 1/31/02 12:26 PM Page 245
Example: Example:
if (length(input) == 0) { input = input.substring(0,3) + "-" +
input.substring( 3,4);
errorMessage = "Please enter a product
code.";
When validating a list of values, you may want to
If you do not want to take case into consideration use a custom function. See Chapter 9 for more
when you evaluate an entry made by the user, information on custom functions.
use the toLowerCase or toUpperCase function
to convert the input value before you make your
comparison. The example shown here converts
the input to lowercase and then compares the
input to a lowercase value.
Example:
if (input.toLowerCase().substring(0, 1 ) != "p")
Á Check a substring to ° Send error messages to the · Move to the test ‚ Test your movie by first
ensure that the input value is user. environment. entering a product code.
in a valid list of values.
■ Associate the variable with Note: See page 38 for instructions on — Click the Submit button.
‡ Check a substring to a dynamic text box. This how to test your movie.
ensure that the value is a enables you to display the ■ The script validates user
number. value that the variable returns entries.
to the user.
245
3657-5 Ch12.F 1/31/02 12:26 PM Page 246
FLASH ACTIONSCRIPT
VALIDATE A DATE
he 12 months of the year are usually numbered from 1 November do not exceed 30. February is limited to 28 days,
VALIDATE A DATE
Actions Ctrl+Alt+A
⁄ Select the frame, button, ¤ Click Window ➪ Actions ‹ Validate the month. ˇ Check whether the user
or movie clip to which you to open the Actions panel. made an entry.
want to add ActionScript. › Validate the day.
246
3657-5 Ch12.F 1/31/02 12:26 PM Page 247
Á Send error messages. ‡ Perform a calculation. ° Move to the test ‚ Click the Submit button.
environment.
■ The script will validate
Note: See page 38 for instructions on your entry before calculating
how to test your movie. your age.
· Test your movie by
entering your birthday.
247
3657-5 Ch12.F 1/31/02 12:26 PM Page 248
FLASH ACTIONSCRIPT
text = "George Bill Tom Peter Bill George Allen Fred Bill Fred Peter George
Actions Ctrl+Alt+A
⁄ Select the frame, button, ¤ Click Window ➪ Actions ‹ Assign text to a variable. ˇ Initialize the variable.
or movie clip to which you to open the Actions panel.
want to add ActionScript. › Associate the variable ■ This example uses the
with a text box. startV variable to specify
Note: This example uses file search.fla, where the search should start.
which you can find on the CD-ROM The variable StartV is used
that accompanies this book. as the indexOf start
argument.
248
3657-5 Ch12.F 1/31/02 12:26 PM Page 249
Example:
on (release) {
valueV =
text.toLowerCase().indexOf(searchV.toLowerCase(),
startV ) ;
if (ValueV != -1) {
Selection.setFocus("text");
Selection.setSelection(valueV,valueV +
length(searchV.toLowerCase()));
startV = valueV + 1;
messageV = "";
} else {
startV = 0;
messageV = "End of text. Entry not found. Click
again to start at beginning.";
}
}
Á Select the frame, button, ‡ Get the indexOf search ° Check to determine if the
or movie clip to which you value and associate the value of searchv is found.
want to add ActionScript. variable with an input text
box. · If ActionScript finds the
search value, set the focus.
■ Use searchv to get the
value for which the user is CONTINUED
searching.
249
3657-5 Ch12.F 1/31/02 12:26 PM Page 250
FLASH ACTIONSCRIPT
‚ Define the selection ± Use indexV plus the ■ This causes the next £ If ActionScript cannot find
criteria. length of the search criteria search to begin one character the search value, reset the
as the end argument for to the right of the last value variable.
— Use IndexV as the start Selection found.
argument for Selection .setSelection. ■ This causes startV to
.setSelection. ™ Clear any messages begin the next search at the
¡ Add 1 to the indexV previously issued. top of the text box.
250 value.
3657-5 Ch12.F 1/31/02 12:26 PM Page 251
Example:
on (release) {
if (text.lastIndexOf(searchV, startV ) != -1)
{
Selection.setFocus("text");
Selection.setSelection(text.lastIndexOf(search
V, startV ),text.lastIndexOf(searchV, startV ) +
length(searchV));
startV = text.lastIndexOf(searchV, startV )
- 1;
messageV = "";
} else {
startV = 0;
startV = length(text);
messageV = "Begining of text. Entry not
found. Click button to start again.";
}
}
¢ Send a message to the ∞ Move to the test § Test your movie by first
user. environment. entering a name.
Note: See page 38 for instructions on ¶ Click the Search button.
how to test your movie.
■ ActionScript searches for
the value you entered.
251
3657-5 Ch12.F 1/31/02 12:26 PM Page 252
FLASH ACTIONSCRIPT
CONVERT SYMBOLS
uttons have several handlers that you can use to A handy technique for creating continuous play actions
Creating a button that executes a continuous action is a To use these actions in combination with one another, you
good example of when you need to convert a button to a must create the button that starts and stops the action, and
movie clip. If you want to create a button that executes a then you must convert the button to a movie clip. You use
continuous action, such as rotating an object when the user the Symbol Properties dialog box to convert a button to a
clicks the object and continuing to rotate the object as long movie clip.
as the user holds down the mouse, you need to use the
button on(press) and on(release) handlers and the Creating an object that users can click and drag is another
movie clip onClipEvent(enterFrame) handler. common example of when you need to convert a button
into a movie clip. For more information on creating a
The on(press) handler triggers actions when the user draggable object, see Chapter 2.
clicks the object. The on(release) handler triggers
actions when the user releases the mouse. You use
on(press) to start a continuous action and on(release)
to stop a continuous action.
CONVERT SYMBOLS
Convert to Symbol... F8
252
3657-5 Ch12.F 1/31/02 12:26 PM Page 253
› Type a symbol name. Á Click OK. ‡ Select the movie clip. ° Add ActionScript.
ˇ Click Movie Clip. ■ The button is now a movie
clip.
253
3657-5 Ch12.F 1/31/02 12:27 PM Page 254
FLASH ACTIONSCRIPT
onClipEvent (enterFrame) {
Actions Ctrl+Alt+A
⁄ Select the frame, button, ¤ Click Window ➪ Actions ‹ Select the event handler.
or movie clip to which you to open the Actions panel.
want to add ActionScript.
Note: This example uses file rotate.fla,
which you can find on the CD-ROM
that accompanies this book.
254
3657-5 Ch12.F 1/31/02 12:27 PM Page 255
Example:
on (rollOver) {
press = true;
}
on (rollOut) {
press = false;
}
Example:
onClipEvent (enterFrame) {
if (press == true) {
_root.frogMC._rotation =_root.frogMC._rotation + 10;
}
}
› Set the rotation. ˇ Repeat steps 3 and 4 for Á Move to the test ‡ Test your movie by
additional handlers. environment. clicking the mouse, moving
the mouse, and rolling the
Note: See page 38 for instructions on cursor over the button to test
how to test your movie. the mouse handlers.
255
3657-5 Ch12.F 1/31/02 12:27 PM Page 256
FLASH ACTIONSCRIPT
⁄ Select the frame, button, ¤ Click to open the › Select the frame, button, ˇ Assign the color of the
or movie clip to which you Actions panel. or movie clip to which you button to a variable.
want to add ActionScript. want to add ActionScript.
‹ Assign a color to a color
Note: This example uses file color.fla, variable.
which you can find on the CD-ROM
that accompanies this book. Note: This step initializes the color.
ActionScript applies this color if the
user does not select a color.
256
3657-5 Ch12.F 1/31/02 12:27 PM Page 257
Frame 1
colorChanger = new Color( _root.circleMC );
colorChanger.setRGB(0xff0000);
Button
on (release) {
if (colorChanger.getRGB() == parseInt(0xff0000)) {
colorChanger.setRGB(0x0000FF);
} else if (colorChanger.getRGB() == parseInt(0x0000ff)) {
colorChanger.setRGB(0xFFFF00);
} else if (colorChanger.getRGB() == parseInt(0xffff00)) {
colorChanger.setRGB(0x009900);
} else if (colorChanger.getRGB() == parseInt(0x009900)) {
colorChanger.setRGB(0xff0000);
}
}
Convert the button to a movie clip. Give the movie clip the name that you targeted
in frame 1. In this example, the name is circleMC.
Á Select the frame, button, · Use the variable that you ± Move to the test ™ Click the object.
or movie clip to which you created to assign a color to environment.
want to add ActionScript. the Color object. ■ The script enables the user
Note: See page 38 for instructions on to click a color palette to set
‡ Create a Color object. ‚ Convert the button to a how to test your movie. a color and then make an
movie clip. object that color.
° Target a movie clip. ¡ Test your movie by first
— Name the movie clip, clicking the color palette.
using the name that you
specified in step 8. 257
3657-5 Ch12.F 1/31/02 12:27 PM Page 258
FLASH ACTIONSCRIPT
EMULATE PANELS
any programs use panels to provide users with mouse is released, but the action is not limited to when the
With buttons, you can start the drag action when the user
they are not needed. clicks the object using the On Press handler. You can stop
the drag action with the user releases the mouse or the
Emulating a panel using ActionScript is easy. You create an pointer rolls out of the area of the object by using the
object that is draggable, visible when needed, invisible Release and Roll Out handlers. Therefore, you start
when not needed, and contains a set of options. creating your panel by creating a draggable button that will
act as a panel.
The startDrag and stopDrag actions enable you to
create objects that the user can drag. Both movie clips and Panels provide the user with options or tools that you
buttons can be draggable. However, you want an object that develop. The options can be anything that you want.
is draggable when you click the mouse while the mouse is They can be buttons or movie clips that enable the user to
located over the object and for which the drag action stops perform an action. Create the options or tools and position
when you release the mouse. Movie clips can be draggable them on the panel.
when clicked, and you can stop the drag action when the
EMULATE PANELS
⁄ Create the button that will ¤ Click to open the › Create options and place ■ This example places a
act as the panel. Actions panel. them on the panel. color bar on the panel.
Note: This example uses file panel.fla, ‹ Make the panel draggable. Note: The options can be anything that
which you can find on the CD-ROM you want.
that accompanies this book. Note: See page 56 for more
information on making an
object draggable.
258
3657-5 Ch12.F 1/31/02 12:27 PM Page 259
Panels are objects that you may want to use in When placing tools and options on a panel, make
many of your movies. The common library sure that your path names are correct. If you do not
enables you to easily access objects that you use specify the correct paths, the panel will not function
repeatedly, so you may want to place your panel properly.
in the common library. You may also want to turn
There are a number of standard features that you
your panel into a smart clip. Turning your panel
may want to add to your panel. For example, you
into a smart clip enables you to use the same
may want your panel to be resizable or reappear on
panel in many movies and enables you to adjust
the screen at a specified location.
the features each time you use the panel.
Flash 5 ships with smart clips that enable you to
create menus, radio buttons, and check boxes.
You may find these useful when adding features
to your panel. They are located in the common
library.
ˇ Create a Close button. ‡ Set the movie clip ° Convert the panel button, Note: See page 252 for more
_visible property to Close button, and options to information on converting a button
Note: See page 12 for information on false on release. a movie clip. to a movie clip.
creating a Close button.
Á Select the button.
CONTINUED
259
3657-5 Ch12.F 1/31/02 12:27 PM Page 260
FLASH ACTIONSCRIPT
T when the user does not need the panel. You can use
either the _visible property or the _alpha property
to make an object invisible. However, when you use the
movie clip. Create a button that sets the _visible
property of a movie clip to false when the user clicks the
button. Use the movie clip name that you intend to give to
_alpha property to make an object invisible, the object the panel. Position the button on the panel. Group all the
remains active. If the user clicks in the area in which the objects on the panel and convert everything to a movie clip.
object is located, the object responds. If you use the Then name the instance.
_visible property, the object is inactive when it is not
visible and does not respond when the user clicks in the You now have a draggable panel that the user can hide.
area in which the object is located. When creating a panel, Once hidden, the user needs to be able to return the object
use the _visible property to hide the panel from view to the Stage when needed. You can create a button that
when the user does not need the panel. returns the panel to the screen and place the button on the
Stage. Or instead, if you use a Key method, you can add an
The object that you create to use as a panel is a draggable action to the panel that returns the panel to the Stage when
button. You need to change the _visible property of the the user presses a key. Using a key instead of a button
object. Unfortunately, you cannot change the _visible makes your panel self-contained and reusable. You can of
property of a button. You can only change the properties of course use both a key and a button.
movies and movie clips. Therefore, you must convert the
button and all the options to a movie clip. But first,
· Name the movie clip ‚ Select the movie clip. ± Set the _visible
instance, using the name that property to true when the
you specified in step 7. — Click to open the user presses the Home key.
Actions panel.
260
3657-5 Ch12.F 1/31/02 12:27 PM Page 261
Example:
onClipEvent (keyDown) {
if (Key.isDown(Key.PGUP)) {
setProperty (_root.panelMC, _xscale, 125);
setProperty (_root.panelMC, _yscale, 125);
}
}
onClipEvent (keyDown) {
if (Key.isDown(Key.PGDN)) {
setProperty (_root.panelMC, _xscale, 75);
setProperty (_root.panelMC, _yscale, 75);
}
}
onClipEvent (keyDown) {
if (Key.isDown(Key.END)) {
setProperty (_root.panelMC, _xscale, 100);
setProperty (_root.panelMC, _yscale, 100);
}
}
onClipEvent (enterFrame) {
¡ Click Control ➪ Test ™ Test your movie by first ¢ Press the Home key to
Movie to move to the test dragging the panel. open the panel.
environment.
£ Click to close the
panel. 261
3657-5 Ch12.F 1/31/02 12:27 PM Page 262
FLASH ACTIONSCRIPT
In trigonometry, you use radians to measure the size of _sampleMC._x = 200 + Math.cos(-s)*100;
angles. You use the formula Math.PI/180 * degrees to _sampleMC._y = 200 + Math.sin(-s)*100;
convert degrees to radians. Although not essential, you may
⁄ Select the frame, button, ‹ Click to open the Á Move to the test ‡ Test your movie.
or movie clip to which you Actions panel. environment.
want to add ActionScript. ■ The movie clip moves in a
› Use the enterFrame Note: See page 38 for instructions on clockwise direction.
Note: This example uses file trig_1.fla, handler. how to test your movie.
which you can find on the CD-ROM
that accompanies this book. ˇ Move the object
clockwise.
¤ Name a movie clip
instance.
262
3657-5 Ch12.F 1/31/02 12:27 PM Page 263
Example:
redMC
onClipEvent (enterFrame) {
radianV += Math.PI/180*2;
_root.redMC._y = 200 + Math.sin(radianV* 5)*50;
_root.redMC._x = 200 + Math.cos(radianV* 1)*200;
}
Example:
yellowMC
onClipEvent (enterFrame) {
radianV += Math.PI/180*2;
_root.yellowMC._y = 200 + Math.sin(radianV* 5)* 50;
_root.yellowMC._x = 200 + Math.cos(radianV* 2)* 200;
}
⁄ Select the frame, button, ‹ Click to open the Á Move to the test ‡ Test your movie.
or movie clip to which you Actions panel. environment.
want to add ActionScript. ■ The movie clip moves
› Use the enterFrame Note: See page 38 for instructions on back and forth.
Note: This example uses file trig_2.fla, handler. how to test your movie.
which you can find on the CD-ROM
that accompanies this book. ˇ Move the object back
and forth.
¤ Name a movie clip
instance. CONTINUED
263
3657-5 Ch12.F 1/31/02 12:27 PM Page 264
FLASH ACTIONSCRIPT
DEMYSTIFY TRIGONOMETRIC
FUNCTIONS (CONTINUED)
o move the object back and forth, use the formula for To increase the speed, multiply s by the factor by which you
To move the object up and down, use the formula for the To cause the object to move across the stage in a wave-like
_y coordinate, but do not use the formula for the _x motion, increase the area value of _x and the speed of _y:
coordinate — for example:
sampleMC._x = 200 + Math.cos(s)*300;
_sampleMC._y = 200 + Math.sin(s)*100; sampleMC._y = 200 + Math.sin(s*10)*100;
To have the object move diagonally, use the same To cause the object to move up and down the Stage in a
trigonometric function for both the _x and the _y wave-like motion, increase the area value of _y and the
coordinates, as shown here: speed of _x, as shown in the following:
_sampleMC._x = 200 + Math.cos(s)*100; sampleMC._x = 200 + Math.cos(s * 10)*100;
_sampleMC._y = 200 + Math.cos(s)*100; sampleMC._y = 200 + Math.sin(s)*300;
⁄ Select the frame, button, ‹ Click to open the Á Move to the test ‡ Test your movie.
or movie clip to which you Actions panel. environment.
want to add ActionScript. ■ The movie clip moves
› Use the enterFrame Note: See page 38 for instructions on diagonally.
Note: This example uses file trig_3.fla, handler. how to test your movie.
which you can find on the CD-ROM
that accompanies this book. ˇ Move the object
diagonally.
¤ Name a movie clip
instance.
264
3657-5 Ch12.F 1/31/02 12:27 PM Page 265
Example:
redMC
onClipEvent (enterFrame) {
radianV += Math.PI/180*2;
_root.redMC._x = 200 + Math.cos(radianV)*100;
}
Example:
yellowMC
onClipEvent (enterFrame) {
radianV += Math.PI/180*2;
_root.yellowMC._y = 200 + Math.sin(radianV)* 100;
_root.yellowMC._x = 200 + Math.cos(radianV)* 100;
}
⁄ Select the frame, button, ‹ Click to open the Á Move to the test ‡ Test your movie.
or movie clip to which you Actions panel. environment.
want to add ActionScript. ■ The movie clip moves in a
› Use the enterFrame Note: See page 38 for instructions on wave-like motion.
Note: This example uses file trig_4.fla, handler. how to test your movie.
which you can find on the CD-ROM
that accompanies this book. ˇ Move the object in a
wave-like motion.
¤ Name a movie clip
instance. 265
3657-5 Ch12.F 1/31/02 12:27 PM Page 266
FLASH ACTIONSCRIPT
Y
movieClip._property = Math.sin(s)*a
with the _xscale, _yscale, _height, _width, movieClip._property = Math.cos(s)*a
_rotation, and _alpha properties to create special
effects. The _height and _width properties change the The s represents speed. The a represents area. Use
height and width of a movie clip using pixels. The _xscale movieClip to specify the name of the movie clip whose
and _yscale properties change the height and width of a property you want to change. Use s to adjust the speed at
movie clip using a percentage. Because these two methods which the property changes. The higher the value of s, the
change the size of a movie clip using two different methods, faster the property will change. Use a with the _height,
they produce slightly different results. _width, _xscale, and _yscale properties to determine
the maximum size of the object. Remember, _height and
The _xscale property can produce the illusion of an _width use pixels, and _xscale and _yscale use a
object flipping from left to right. The _yscale property percentage. Use a with the rotation to adjust the number
can produce the illusion of an object flipping up and down. of rotations the object spins clockwise before spinning
The _height and _width properties shrink and grow an counterclockwise.
object. The _rotation property produces a spinning
effect. The object spins in one direction and then spins in You may use the formula Math.PI/180 * 2 as a base
the opposite direction. The _alpha property fades color in from which you adjust the speed of your object.
and out.
You can use the following formulas when using sin or cos
with these properties:
⁄ Select the frame, button, ¤ Name the instance. › Use the enterFrame ˇ Set the base speed.
or movie clip to which you handler.
want to add ActionScript. Note: See page 10 for more
information on naming an instance.
‹ Click to open the
Actions panel.
266
3657-5 Ch12.F 1/31/02 12:27 PM Page 267
Example:
onClipEvent (enterFrame) {
speedV += Math.PI/180 *2;
_root.blockMC._height = Math.cos(speedV * 3 )* 100;
_root.blockMC._width = Math.sin(speedV * 3 )* 100;
_root.blockMC._rotation = Math.sin(speedV * 3 )* 100;
_root.blockMC._alpha = Math.sin(speedV * 3 )* 100;
}
Á Use sin to set the ° Move to the test · Test your movie. ■ Your movie clip will scale
_xscale. environment. and rotate.
‡ Use sin to set the Note: See page 38 for instructions on
_rotation. how to test your movie.
267
3657-5 Ch13.F 1/31/02 12:27 PM Page 268
FLASH ACTIONSCRIPT
Debugger
268
3657-5 Ch13.F 1/31/02 12:27 PM Page 269
DEBUGGING ACTIONSCRIPT 13
The Debugger uses a special version of Flash Player — the Flash To add a variable to the Watch list,
Debug Player — that installs automatically when you install select the variable, click in
Flash 5. To activate the Flash Debug Player, click Control ➪ the upper-right corner of the
Debug Movie. Debugger panel to open the
menu, and then click Add Watch.
You can use the Flash Debug Player as a standalone player. If
To remove a variable from the
you choose Debugging Permitted under the Flash tab of the
Watch list, select the variable,
Publish Settings dialog box, when you publish your Flash movie,
click , and then click Remove
you can use the Debugger as you play your movie. Just click
Watch.
Control ➪ Debugger in the Flash Debug Player menu. When
using Debugger with the Flash Debug Player, Flash must be
open. You may want to password-protect your movie because
anyone viewing your movie using the Flash Debug Player will
also be able to view your property and variable settings. The
Flash tab of the Publish Settings dialog box provides an option
that enables you to password-protect the Debugger option.
You can download the latest version of the Flash Debug Player
from the Macromedia Web site. A link to the site is included on
the CD-ROM that accompanies this book.
_level0 _level0
_alpha 100
‹ Click a level or instance › Click the Properties tab to Á Click the Variables tab to ■ You can use Debugger to
to select it. view properties. view the variables. troubleshoot your movies.
ˇ Double-click a value and ‡ Double-click a value and
type to change a property. type to change a variable.
269
3657-5 Ch13.F 1/31/02 12:27 PM Page 270
FLASH ACTIONSCRIPT
Layer 1
Output
USING THE TRACE ACTION ¤ Click to open the ˇ Click Window ➪ Output
Actions panel. to open the Output window.
⁄ Select the frame, button,
or movie clip to which you ‹ Use the trace action to
want to add ActionScript. evaluate an expression.
Note: This example uses file › Move to the test
trace.fla, which you can find on environment.
the CD-ROM that accompanies
this book.
270
3657-5 Ch13.F 1/31/02 12:27 PM Page 271
DEBUGGING ACTIONSCRIPT 13
The script shown here uses the trace action to
display changes made to an array.
Script:
on (release) {
name = ["George”, "John", "James"];
trace (name);
name[1] = "Chris";
trace (name);
name.push("Gene", "Charles", "Sue" );
trace (name);
}
Options
Á Click the button to test ■ Each time you click the THE OPTIONS MENU ■ You can use the options
your movie. button, the ouput window menu to copy the contents
will display the results of
⁄ Click Options to open the of the Output window to the
Options menu.
the trace. clipboard, clear the Output
¤ Select an option. window, save the contents of
the Output window to a file,
or print the contents of the
output window.
271
3657-5 Ch13.F 1/31/02 12:27 PM Page 272
FLASH ACTIONSCRIPT
Actions
⁄ Create your movie. ¤ Click Control ➪ Test ‹ Click Debug ➪ List ■ The results display in the
Movie to move to the test Objects. Output window.
environment.
272
3657-5 Ch13.F 1/31/02 12:27 PM Page 273
DEBUGGING ACTIONSCRIPT 13
Actions
⁄ Create your movie. ¤ Click Control ➪ Test ‹ Click Debug ➪ List ■ The results display in the
Movie to move to the test Variables. Output window.
environment.
273
3657-5 Ch13.F 1/31/02 12:27 PM Page 274
FLASH ACTIONSCRIPT
As you create your scripts, you should save often, giving Give the variables, arrays, functions, and objects that you
each save a different version number. If your script gets create meaningful names. This will make it easier for you
extremely complex and stops working, this enables you to and others who view your code to understand your code.
go back to the last working version of the script.
Develop a naming convention and stick with it. For
If a portion of script does not work, try breaking the script example, you can type the first word in a variable name in
down into component parts. This enables you to narrow small letters, and subsequent words can start with capital
things down so that you can determine which portion of letters. The variable name playerFirstName is an
the script is not working. You can also comment out example of this convention.
portions of the script and test it. Again, this helps narrow
down where the error is located. When trying to understand a movie written by someone
else, use Movie Explorer to find and view ActionScript.
Use comments to document your code. This is very
important if you are working with other developers. Even if
Colored Syntax
⁄ Add comments. ¤ Add meaningful names. › Click ➪ Colored Syntax ˇ Click ➪ Check Syntax
to use colored syntax. to perform a syntax check.
‹ Add a naming convention.
274
3657-5 Ch13.F 1/31/02 12:27 PM Page 275
DEBUGGING ACTIONSCRIPT 13
275
3657-5 Ch14.F 1/31/02 12:27 PM Page 276
FLASH ACTIONSCRIPT
276
3657-5 Ch14.F 1/31/02 12:27 PM Page 277
FUNCTION KEYS
OTHER KEYS
277
3657-5 Ch14.F 1/31/02 12:27 PM Page 278
FLASH ACTIONSCRIPT
278
3657-5 Ch14.F 1/31/02 12:27 PM Page 279
LIST OF KEYWORDS
Keywords are words that
ActionScript reserves for
specific use within the break continue delete else
language. You cannot use
keywords to name for function if in
variables, functions, or new return this typeof
labels. The following is a list
of ActionScript keywords. var void while with
279
3657-5 Ch14.F 1/31/02 12:27 PM Page 280
FLASH ACTIONSCRIPT
280
3657-5 Ch14.F 1/31/02 12:27 PM Page 281
281
3657-5 Ch14.F 1/31/02 12:27 PM Page 282
FLASH ACTIONSCRIPT
282
3657-5 Ch14.F 1/31/02 12:27 PM Page 283
Y button
handlers to
instruct Flash on
HANDLER PURPOSE
how to respond on(release) Performs specified actions when the pointer is over the button and the
when the user user releases the mouse. This is the default handler.
clicks, moves the
mouse over, or on(press) Performs specified actions when the pointer is over the button and
interacts in some the user presses the mouse.
other way with a on(releaseOutside) Performs specified actions when the user drags the pointer outside the
button. The table button area and releases the mouse.
that follows lists
on(rollOver) Performs specified actions when the user rolls the pointer over the
button handlers
button.
and their
purpose. on(rollOut) Performs specified actions when the user rolls the pointer over and
then outside of the button area.
on(dragOver) Performs specified actions when the user clicks the button, drags the
pointer away from the button, and then drags the pointer back over
the button.
on(dragOut) Performs specified actions when the pointer is over the button and the
user presses the mouse and then drags the pointer outside the button
area.
on(keyPress, "key") Performs specified actions when the user presses a specified key.
Y movie clip
handlers to
instruct Flash on
HANDLER PURPOSE
how to respond onClipEvent(load) Performs the specified actions when the movie clip appears on the
when the user Timeline. This is the default handler.
loads or unloads
onClipEvent(unload) Performs the specified actions in the first frame after you remove the
a movie clip,
movie clip from the Timeline.
enters a frame,
moves the onClipEvent(enterFrame) Performs the specified actions as a frame plays.
mouse, presses a onClipEvent(mouseMove) Performs the specified actions every time the user moves the mouse.
key, or interacts
in some other onClipEvent(mouseDown) Performs the specified actions when the user presses the mouse.
way with a movie onClipEvent(mouseUp) Performs the specified actions when the user releases the mouse.
clip. The table
that follows lists onClipEvent(keyDown) Performs the specified actions when the user presses any key.
movie clip onClipEvent(keyUp) Performs the specified actions when the user releases any key.
handlers and
their function. onClipEvent(data) Performs the specified actions when ActionScript receives data.
283
3657-5 Ch14.F 1/31/02 12:27 PM Page 284
FLASH ACTIONSCRIPT
OPERATORS
ou use operators to combine, compare, or modify
284
3657-5 Ch14.F 1/31/02 12:27 PM Page 285
FUNCTIONS
function is a block of script that can be reused. The
285
3657-5 Ch14.F 1/31/02 12:27 PM Page 286
FLASH ACTIONSCRIPT
ACTIONS
ou use actions to send commands in ActionsScript. unload a movie, or perform myriad other tasks. The
Y You can use actions to start or stop a movie, load or following table lists the actions available to you in
ActionScript.
286
3657-5 Ch14.F 1/31/02 12:27 PM Page 287
287
3657-5 Ch14.F 1/31/02 12:27 PM Page 288
FLASH ACTIONSCRIPT
OBJECTS
ARRAY OBJECT
You can use an array to group related data. The table that
follows lists the methods you can use with the Array object.
BOOLEAN OBJECT
COLOR OBJECT
288
3657-5 Ch14.F 1/31/02 12:27 PM Page 289
DATE OBJECT
You can use the Date object to retrieve and set the date.
The table that follows lists the methods and purposes
of the Date object.
289
3657-5 Ch14.F 1/31/02 12:27 PM Page 290
FLASH ACTIONSCRIPT
KEY OBJECT
MATH OBJECT
You can use the methods of the Math object to calculations. The following table lists the methods of the
manipulate numbers and perform mathematical Math object and their purpose.
290
3657-5 Ch14.F 1/31/02 12:27 PM Page 291
MOUSE OBJECT
MOVIECLIP OBJECT
291
3657-5 Ch14.F 1/31/02 12:27 PM Page 292
FLASH ACTIONSCRIPT
NUMBER OBJECT
OBJECT OBJECT
SELECTION OBJECT
292
3657-5 Ch14.F 1/31/02 12:27 PM Page 293
STRING OBJECT
METHOD PURPOSE
charAt Returns the character at a specified index position.
charCodeAt Returns the numeric value of a character at a specified index position.
concat Concatenates strings.
fromCharCode Returns the character assigned to a code.
indexOf Returns the first occurrence of the index value of a substring.
lastIndexOf Returns the last occurrence of the index value of a substring.
slice Returns a substring of a string.
substr Returns a substring of a string. You specify the starting position and length.
substring Returns a substring of a string. You specify the starting and ending position.
toLowerCase Converts a string to lowercase.
toUpperCase Converts a string to uppercase.
293
43657-5 AppEULA.F 1/31/02 12:28 PM Page 294
APPENDIX
WHAT’S ON THE CD-ROM
he CD-ROM disc included in this book contains
SYSTEM REQUIREMENTS not currently have Adobe Acrobat Reader 5 installed, the
computer will prompt you to install the software Acrobat
While most programs on the CD-ROM disc have minimal
files. A freeware version of Adobe Acrobat Reader is also
system requirements, your computer should be equipped
included on the disc.
with the following hardware and software to make the best
use of all the contents of the CD-ROM disc:
INSTALLING AND USING THE SOFTWARE
• A Pentium processor running Windows 95 or later or
Windows NT 4 or later This CD-ROM disc contains several useful programs. Before
installing a program from the CD, you should exit all other
• At least 32 MB of available RAM programs. In order to use most of the programs, you must
• At least 40 MB of available disk space accept the license agreement provided with the program.
Make sure you read any ReadMe files provided with each
• Microsoft Internet Explorer 4.0 or later, or Netscape
program.
Navigator 4.0 or later recommended
• 800 x 600 color display
Program Versions
• CD-ROM drive Shareware programs are fully functional, free trial versions
of copyrighted programs. If you like a particular program,
AUTHOR’S SOURCE CODE you can register with its author for a nominal fee and
receive licenses, enhanced versions, and technical support.
For Windows 2000. The CD provides files that contain all
the sample code used throughout this book. You can browse Freeware programs are free, copyrighted games, applications,
these files directly from the CD-ROM, or you can copy and utilities. You can copy them to as many computers as
them to your hard drive and use them as the basis for your you like, but they have no technical support.
own projects. To find the files on the CD-ROM, open the
D:\RESOURCES\CODE folder. To copy the files to your hard GNU software is governed by its own license, which is
drive, just run the installation program D:\RESOURCES\ included inside the folder of the GNU software. There are
CODE.EXE. The files will be placed on your hard drive at no restrictions on distribution of this software. See the GNU
C:\ProgramFiles\FlashActionScript. After installing, you can license for more details.
access the files from the Start menu. You will need Flash 5
installed on the machine to run the samples. Please see Using Trial, demo, or evaluation versions are usually limited either
Creative Techniques in Chapter 12 for more information. by time of functionality. For example, you may not be able
to save projects using these versions.
ACROBAT VERSION For your convenience, the software titles on the CD are
listed in alphabetic order.
The CD-ROM contains an e-version of this book that you
can view and search using Adobe Acrobat Reader. You can
also use the hyperlinks provided in the text to access all Acrobat Reader
Web pages and Internet references in the book. You cannot For Mac and Windows. Freeware. Acrobat Reader lets you
print the pages or copy text from the Acrobat files. If you do view the online version of this book. For more information
294
43657-5 AppEULA.F 1/31/02 12:28 PM Page 295
FLASH ACTIONSCRIPT:
Your visual blueprint for
creating Flash-enhanced Web sites
on using Adobe Acrobat Reader, see page 296. From Adobe Stuffit Expander
Systems, Inc., www.adobe.com. For Mac. Commercial version. Enables users to access all
downloads and attachments. From Aladdin Systems,
www.aladdinsys.com.
Adobe Photoshop
For Mac and Windows. Trial Version. Enables you to create,
edit, or retouch images. From Adobe Systems, Inc., Stuffit Lite
www.adobe.com. For Mac. Shareware. Enables you to compress files and open
compressed files. From Aladdin Systems,
www.aladdinsys.com/.
Dreamweaver 4
For Mac and Windows. Trial version. Enables you to quickly
and easily develop HTML-based Web pages. From WinZip
Macromedia, Inc., www.macromedia.com. For Windows. Shareware. Enables you to compress files and
open compressed files. From Nico Mak Computing, Inc.,
www.winzip.com/.
Dreamweaver UltraDev 4
For Mac and Windows. Trial Version. Enables you to develop
Web pages that include ASP, JSP, or ColdFusion. From TROUBLESHOOTING
Macromedia, Inc., www.macromedia.com.
We tried our best to compile programs that work on most
computers with the minimum system requirements. Your
Flash 5 computer, however, may differ and some programs may not
For Mac and Windows. Trial Version. Enables you to create work properly for some reason.
interactive multimedia Web pages. From Macromedia, Inc.,
www.macromedia.com. The two most likely problems are that you don’t have enough
memory (RAM) for the programs you want to use, or you
have other programs running that are affecting installation
Flash Player or running of a program. If you get error messages like Not
For Mac and Windows. Commercial version. Enables you to enough memory or Setup cannot continue, try one
view Macromedia Flash content. From Macromedia, Inc., or more of these methods and then try using the software
www.macromedia.com. again:
Shockwave
For Mac and Windows. Trial Version. Enables you to create
multimedia content, including advanced 3D games. From
Macromedia, Inc., www.macromedia.com.
295
43657-5 AppEULA.F 1/31/02 12:28 PM Page 296
APPENDIX
FLIP THROUGH PAGES First page ZOOM IN ■ Click one of these options
to display the page at 100%
⁄ Click one of these options Previous page ⁄ Click to magnify an
magnification ( ) or to fit
to flip through the pages of a area of the page.
Next page the entire page inside the
section.
¤ Click the area of the page window ( ).
Last page you want to magnify.
296
43657-5 AppEULA.F 1/31/02 12:28 PM Page 297
FLASH ACTIONSCRIPT:
Your visual blueprint for
creating Flash-enhanced Web sites
To install Acrobat Reader, insert the CD-ROM Acrobat Reader is a popular and useful program.
disc into a drive. In the screen that appears, click There are many files available on the Web that are
Software. Click Acrobat Reader and then click designed to be viewed using Acrobat Reader.
Install at the bottom of the screen. Then follow Look for files with the .pdf extension. For more
the instructions on your screen to install the information about Acrobat Reader, visit the
program. Web site at www.adobe.com/products/
acrobat/readermain.html.
You can make searching the book more
convenient by copying the .pdf files to your own
computer. Display the contents of the CD-ROM
disc and then copy the PDFs folder from the CD
to your hard drive. This allows you to easily
access the contents of the book at any time.
FIND TEXT ¤ Type the text you want to ■ The first instance of the › Click Find Again to find
find. text is highlighted. the next instance of the text.
⁄ Click to search for text
in the section. ‹ Click Find to start the
search.
■ The Find dialog box
appears.
297
43657-5 AppEULA.F 1/31/02 12:28 PM Page 298
APPENDIX
HUNGRY MINDS, INC. (b) You may not reverse engineer, decompile, or
END-USER LICENSE AGREEMENT disassemble the Software. You may transfer the Software
and user documentation on a permanent basis, provided
that the transferee agrees to accept the terms and
READ THIS. You should carefully read these terms and
conditions of this Agreement and you retain no copies. If
conditions before opening the software packet(s) included
the Software is an update or has been updated, any transfer
with this book ("Book"). This is a license agreement
must include the most recent update and all prior versions.
("Agreement") between you and Hungry Minds, Inc.
("HMI"). By opening the accompanying software packet(s), 4. Restrictions on Use of Individual Programs. You must
you acknowledge that you have read and accept the follow the individual requirements and restrictions detailed
following terms and conditions. If you do not agree and do for each individual program in the What’s on the CD-ROM
not want to be bound by such terms and conditions, appendix of this Book. These limitations are also contained
promptly return the Book and the unopened software in the individual license agreements recorded on the
packet(s) to the place you obtained them for a full refund. Software Media. These limitations may include a
requirement that after using the program for a specified
1. License Grant. HMI grants to you (either an individual
period of time, the user must pay a registration fee or
or entity) a nonexclusive license to use one copy of the
discontinue use. By opening the Software packet(s), you will
enclosed software program(s) (collectively, the "Software")
be agreeing to abide by the licenses and restrictions for
solely for your own personal or business purposes on a
these individual programs that are detailed in the What’s on
single computer (whether a standard computer or a
the CD-ROM appendix and on the Software Media. None
workstation component of a multi-user network). The
of the material on this Software Media or listed in this Book
Software is in use on a computer when it is loaded into
may ever be redistributed, in original or modified form, for
temporary memory (RAM) or installed into permanent
commercial purposes.
memory (hard disk, CD-ROM, or other storage device).
HMI reserves all rights not expressly granted herein. 5. Limited Warranty.
2. Ownership. HMI is the owner of all right, title, and (a) HMI warrants that the Software and Software Media
interest, including copyright, in and to the compilation of are free from defects in materials and workmanship under
the Software recorded on the disk(s) or CD-ROM normal use for a period of sixty (60) days from the date of
("Software Media"). Copyright to the individual programs purchase of this Book. If HMI receives notification within
recorded on the Software Media is owned by the author or the warranty period of defects in materials or workmanship,
other authorized copyright owner of each program. HMI will replace the defective Software Media.
Ownership of the Software and all proprietary rights
relating thereto remain with HMI and its licensers. (b) HMI AND THE AUTHOR OF THE BOOK DISCLAIM
ALL OTHER WARRANTIES, EXPRESS OR IMPLIED,
3. Restrictions On Use and Transfer. INCLUDING WITHOUT LIMITATION IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
(a) You may only (i) make one copy of the Software for
A PARTICULAR PURPOSE, WITH RESPECT TO THE
backup or archival purposes, or (ii) transfer the Software to
SOFTWARE, THE PROGRAMS, THE SOURCE CODE
a single hard disk, provided that you keep the original for
CONTAINED THEREIN, AND/OR THE TECHNIQUES
backup or archival purposes. You may not (i) rent or lease
DESCRIBED IN THIS BOOK. HMI DOES NOT WARRANT
the Software, (ii) copy or reproduce the Software through a
THAT THE FUNCTIONS CONTAINED IN THE SOFTWARE
LAN or other network system or through any computer
WILL MEET YOUR REQUIREMENTS OR THAT THE
subscriber system or bulletin-board system, or (iii) modify,
OPERATION OF THE SOFTWARE WILL BE ERROR FREE.
adapt, or create derivative works based on the Software.
298
43657-5 AppEULA.F 1/31/02 12:28 PM Page 299
FLASH ACTIONSCRIPT:
Your visual blueprint for
creating Flash-enhanced Web sites
(c) This limited warranty gives you specific legal rights, and 8. General. This Agreement constitutes the entire
you may have other rights that vary from jurisdiction to understanding of the parties and revokes and supersedes all
jurisdiction. prior agreements, oral or written, between them and may
not be modified or amended except in a writing signed by
6. Remedies. both parties hereto that specifically refers to this
Agreement. This Agreement shall take precedence over any
(a) HMI’s entire liability and your exclusive remedy for other documents that may be in conflict herewith. If any
defects in materials and workmanship shall be limited to one or more provisions contained in this Agreement are
replacement of the Software Media, which may be returned held by any court or tribunal to be invalid, illegal, or
to HMI with a copy of your receipt at the following address: otherwise unenforceable, each and every other provision
Software Media Fulfillment Department, Attn.: Flash shall remain in full force and effect.
ActionScript: Your visual blueprint for creating Flash-
enhanced Web sites, Hungry Minds, Inc., 10475 Crosspoint
Blvd., Indianapolis, IN 46256, or call 1-800-762-2974. Please
allow four to six weeks for delivery. This Limited Warranty is
void if failure of the Software Media has resulted from
accident, abuse, or misapplication. Any replacement
Software Media will be warranted for the remainder of the
original warranty period or thirty (30) days, whichever is
longer.
299
43657-5 Index.F 1/31/02 12:28 PM Page 300
INDEX
A movie clips
association, 6
absolute target path, 224–225 assignment, 29
access [ ] operator, arrays, 162–163 referencing by name, 19
Actions panel movies, adding, 244
Colored Syntax colors, 27 predefined objects, 144
comments, adding, 42–43 smart clips, 18, 236–239
Expert mode, 24–27 statements, 24–27, 36–37
fonts, sizing, 27 static images, non-support, 8
#include statement, 25 view, 275
line, moving to, 27 active layer, 4–5
modes, switching between, 26 Add a Statement button, 24, 26
movies, quality settings, 50–51 Add Scene icon, 3
Normal mode, 24–27 add
open, 270 comments, 42–43
playhead, positioning, 48–49 numeric values, 112–113
scripts, printing, 27 scenes, 3
Show Deprecated Syntax, 27 sounds, 16–17
statements, 24–25, 27, 37 addition (+) operator, 88, 100, 110
string characters, find/replace, 27 adjust, transparency, 68–69
strings, search, 27 alignments, 20–21
syntax errors, checking, 27 alpha, instances, 10–11
Toolbox list, 24–27 ampersand (&) character, bitwise AND, 100
actions reference, 286 AND (&&) operator, 132–133
Actions statement, 24 animated buttons, 12, 19
actions animations
appended with NUM, 280 buttons, 8
argument association, 36 frame-by-frame, 14
buttons, ActionScript definitions, 12 movie clips
test, 38 conversion, 19
ActionScript reusable, 8
Actions panel, 24–27 preloader, 62
argument, 36–37 QuickTime, 32
AS file extension, 25 start/end keyframes, 14
buttons tween adjustments, 15
action definitions, 12 tweened, 14
assignment, 28 antialiasing
association, 6 movie clip quality, 86–87
code blocks, curly braces {and} characters, 36–37 movie quality, 50–51
comments, 42–43 smoothing text, 21
expressions, 37 arguments
frames, assignment, 30–31 comma (,) character as separator, 36
handlers, 28–29 defined, 36
#include statement, 25 parentheses (and) characters, 36
keywords, case sensitive, 36 statements, 24
value assignments, 36
300
43657-5 Index.F 1/31/02 12:28 PM Page 301
FLASH ACTIONSCRIPT:
Your visual blueprint for
creating Flash-enhanced Web sites
arrays behaviors
access operator [ ], 162–163 instances, 11
comma (,) character as separator, 92 movie clips, selecting, 18
conversions, string, 174–175 symbols, 9
create, 162–165 BEST quality, movie clips, 86–87
data types, 93 binary operators, operands, 110
described, 162 bitmap images, movies, printing as, 60
display Boolean
changes, 13 conversions, 108–109
values, 273 data type, 88
elements primitive data type, 92
adding, 168–169 values, false, 56
assigning, 164–165 borders, 22–23
number (index), 162–163 boundary
removing, 170–171 movie clip, 152–153
value changes, 164–165 movie display, 33
empty, 164–165 brackets [and] characters, array access operator, 162–163
extract, 172–173 brightness, instances, 10–11
length, finding, 166–167 broken line, tween start indicator, 15
reverse, 172–173 Brush tool, size/shape, 6
arrow line, tween complete indicator, 15 buttons
Arrow tool, 6–7, 9, 11 ActionScript, 28
AS file extension, 25 animated, create, 19
ASCII characters, URL-encoded conversion, 95 antialiasing on/off, 50–51
ASCII values, 198–200 associations, ActionScript, 6
assign, target paths, 224–225 check mark beside, 13
assignments convert to movie clip, 252
ActionScript, 28–31 enable/disable, 13
variable values, 90–93 execute continuous action, 252
associations four-frame Timeline, 12
buttons, ActionScript, 6 handlers, 252, 283
dynamic text box/variables, 23 images, importing, 13
input text box/variables, 22 instance behavior type, 11
movie clips, ActionScript, 6 interactive graphics, 8–9
operator precedence, 111 keyframe, inserting, 13
asterisk (*) character, 22, 88, 116–117 movie clips, converting to panels, 260
asterisk (*) slash (/) characters, comments, 42 movie startup point, 46
attach, movie clips, 148–151 objects, dragging, 259
rotate continuously, 255
B states, 12–13
background images, symbol advantages, 9 stop action assignment, 44
backgrounds, 22–23 tests, 13, 38
backslash (\) character, escape sequences, 91 Timeline, 8–9
Bandwidth Profiler, movie information display, 40–41
base 16, hexadecimal numbers, 177 C
base, numbers, 215 Caps Lock key, Status bar display, 41
Basic Actions statement, 24 case sensitive, search limitation, 249
301
43657-5 Index.F 1/31/02 12:28 PM Page 302
INDEX
case, strings, changing, 106–107 columns, 5
CGI scripts, load/unload movies, 228–229 comma (,) character
Change the Statement Order button, 24–25 argument separator, 36
change, colors, 176–177 array separator, 92
characters commands
string, extracting, 96–97 Control
URL encoding, 278–279 Debug Movie, 268
check, frame load, 62–63 Loop Playback, 38
check mark, 13, , 27 Mute Sounds, 38–39
child movie clips, parent placement, 234 Play, 38
circles, Oval tool, 6 Play All Scenes, 38
Clip Parameters dialog box, 243 Rewind, 38
Clip Parameters panel, smart clips, 238–241 Step Backward, 38
clocks, 184–185 Step Forward, 38
code Test Movie, 38–40, 43, 261, 272–273
blocks Test Scene, 38–39
comments, 42–43 Debug
curly braces {and} characters, 36–37 List Objects, 272
document with comments, 274 List Variables, 273
hexadecimal color, 280–282 Edit
panel size changes, 261 Copy Frames, 18
collision, detection, 158–159 Deselect All, 19
color boxes, tools, stroke/fill, 6–7 Edit Movie, 9
Color object, movie clips, color change, 176–177 Edit Symbols, 9, 253
Colored Syntax, 27, 274 Paste Frames, 19
coloring book, create, 256–257 Select All, 19
colors File
add to objects, 256–257 Import, 13, 16
Colored Syntax, 27 Print, 61
comments, 27 Publish Preview, Default, 39
deprecated syntax, 27 Publish Settings, 32, 50
fill, 7 Save, 38, 149
fonts, 20–21 Insert
hexadecimal codes, 280–282 Convert to Symbol, 9, 12, 219
hexadecimal values, 176–177 Create Motion Tween, 14
instances, 10–11 Frame, 15
keywords, 27 Keyframe, 13, 30
magenta, comments, 42 Layer, 4, 30
mouse, change, 257 New Symbol, 8, 12–13, 18, 240
movie clips, changing, 176–177 Modify
predefined identifiers, 27 Frame, 15
properties, 27 Instance, 11
quoted strings, 27 Layer, 5
statements, 27 Transform, Edit Center, 73
stroke, 7 Transform, Rotate, 73
transform values, 178–179 Transform, Scale, 79
Options, Define Clip Parameters, 237
302
43657-5 Index.F 1/31/02 12:28 PM Page 303
FLASH ACTIONSCRIPT:
Your visual blueprint for
creating Flash-enhanced Web sites
303
43657-5 Index.F 1/31/02 12:28 PM Page 304
INDEX
objects, draggable, 258 decimal point (.), floating point numbers, 119
rotation effects, 254–255 decrement (—) operator
scenes, 2–3 mathematical data type, 88
scrollable text box, 218–219 unary type, 110
smart clips, described, 236 set values, 124–125
sounds, 202–203 Define Clip Parameters dialog box, 236–239, 243
static text boxes, 20–21 delete
symbol, 8–9 layers, 4–5
trigonometric special effects, 266–267 selected objects, 6
crop, movies, 33 statements, 24–25
curly braces {and} characters, code blocks, 36–37 Delete a Statement button, 25
curves, Pen tool, 6 Delete Scene icon, 3
custom functions, create, 220–221 deprecated syntax, display color, 27
custom interface, smart clips, 239–243 depths, swap, 154–155
detect, collision, 158–159
D device fonts
data types movies, 34
array, 93 static text boxes, 20–21
Boolean, 88 display list
movie clip, 88 Debugger, levels/instances, 268
number, 88 Flash Player, 222–223
object, 88 divide, numeric values, 118–119
primitive, 92 division (/) operator
reference, 92 mathematical data type, 88
string, 88 numeric values, dividing, 118–119
data, validate, 244–245 dot syntax, target path, 224–225
dates double hyphen (—) character, decrement operator, 88, 110,
current, retrieving, 183 124–125
set, 186–187 double plus sign (++), increment operator, 88
update, 189 double slashes (//) characters, single-line comments, 42
validate, 246–247 Down state, buttons, 12–13
values, 188–189 downloads, frames, checking, 62–63
retrieving, 184–185 draggable objects
days, validation, 246–247 create, 56–59
debug, script, 274–275 handlers, 58–59
Debugger scroll bars, 57
display list, 223, 268 sound volume/pan, 57
error correction, 268–269 Dropper tool, fill/stroke attributes, 6–7
movies, activating, 32 duplicate
password protect movies, 269 movie clips, 234–235
Properties tab, changing property values, 268–269 scenes, 3
status bar, 268 Duplicate Scene icon, 3
Variables tab, 268–269 dynamic text
Watch list, add/remove variables, 268–269 invalid data entry, 244
decimal places, round numbers, 194 variables, 88
dynamic text boxes, 23, 208–209
304
43657-5 Index.F 1/31/02 12:28 PM Page 305
FLASH ACTIONSCRIPT:
Your visual blueprint for
creating Flash-enhanced Web sites
INDEX
createarray1.fla, 164 smartclip.fla, 236
date.fla, 182, 186 smartclip2.fla, 242
decrement.fla, 124 sound.fla, 202
divide.fla, 118 sqr.fla, 193
duplicate.fla, 234 substring.fla, 97
elseif.fla, 138 subtract.fla, 114
equality.fla, 128 swap.fla, 154
eval.fla, 213 telltarget.fla, 230
extract.fla, 96 tonumber.fla, 214, 216
for.fla, 142 trig.fla, 192
fsc.fla, 54 trig_1.fla, 262
function.fla, 220 trig_2.fla, 263
getbounds.fla, 152 trig_3.fla, 264
getproperties.fla, 210 trig_4.fla, 265
height2.fla, 76 url.fla, 52
hittest.fla, 158 valstring.fla, 244
if.fla, 134 variable.fla, 90
ifelse.fla, 136 vertimer.fla, 212
increment.fla, 122 visible.fla, 70
joinarray.fla, 174 volppan.fla, 206
jump.fla, 49 while.fla, 140
length.fla, 102, 166 width.fla, 74
LessThan.fla, 126 with.fla, 232
LessThanE.fla, 127 x.fla, 82
load.fla, 226, 228 xscale.fla, 78
localtoglobal.fla, 160 y.fla, 84
logical.fla, 132 yscale.fla, 80
maxmin.fla, 196 FlashPla.exe (PC), 54
modulo.fla, 121 FlashPlayer (Mac), 54
mouse.fla, 180 text, URL encoding, 94
multiply.fla, 116 Fill panel, 7
object.fla, 92, 144 fills, bitmap, 7
panel.fla, 258 find
path.fla, 224 array length, 166–167
poparray.fla, 170 modulo, 120–121
pusharray.fla, 168 numeric values, 196–197
quality.fla, 50, 86 square root, 193
random.fla, 195 fixed width text boxes
rotate.fla, 72, 254 create, 20–21
round.fla, 194 square handle indicator, 20
sales.txt, 94 FLA extension, movies, 32, 39
scroll.fla, 218 Flash, HTML tag support, 23
search.fla, 104, 248 Flash 5, smart clips, 259
selections.fla, 208 Flash Debug Player, password use, 269
setdate.fla, 188 Flash Player
settime.fla, 190 display list, 222–223
slicearray.fla, 172 fscommand action arguments, 54
306
43657-5 Index.F 1/31/02 12:28 PM Page 307
FLASH ACTIONSCRIPT:
Your visual blueprint for
creating Flash-enhanced Web sites
307
43657-5 Index.F 1/31/02 12:28 PM Page 308
INDEX
hexadecimal numbers, color selection, 256 create, 22
hexadecimal values, colors, 176–177 forms, 22
hide HTML tag format, 22
layers, 4–5 multiline, 22
mouse, 180 password, 22
status bar, single line, 22
toolbox, 7 text selections, 208–209
hierarchy, movies, 222–223 user input, 22
HIGH quality, movie clips, 86–87 variable associations, 22
HTML documents, templates, 34 Word Wrap box, 22
HTML tags, 33 insert, layers, 5
HTML Template Info dialog box, 34 Insert Target Path dialog box, 230
hues, instances, 10–11 Instance panel, movie clips, naming/renaming, 19, 66–67
hyphen (-) character instances
numeric values, subtracting, 114–115 behaviors, 11
subtraction operator, 88 movie clips
naming, 19
I targeting, 67
identifiers names, 66–67
colors, 27 symbols, 9–11
movie clip attachment, 148 registration point, 73
images instantiated objects, 144
antialiasing, 50–51 integer numbers, 119
background, 9 integers, numbers, rounding, 194
bitmap, printing movie as, 60 interactive graphics, ActionScript support, 8–9
frame-by-frame animations, 14 interface, 2–3, 239–243
static, 8 Internet Explorer, object tag, 33
tweened frame animations, 14 Item Preview window, symbol preview, 9
import
files, sounds, 16–17 J
images, buttons, 13 JavaScript, messages, passing with fscommand action, 55
increment (++) operator JPEG graphics, quality adjustments, 32
mathematical data type, 88 jumps, 48
unary type, 110
value setting, 122–123 K
indents, static text boxes, 21 key code vales, 276–277
index, array element number, 162–163 keyboards, Key object, 198–201
index position keyframes, 13–14, 30
character string extraction, 96 keys, movie startup point, 46
substring extraction, 98 keywords
inequality (!=) operator, 128–129 case sensitive, 36
infinity, mathematical error evaluation, 216–217 colors, 27
Ink Bottle tool, stroke line color/style, 6 list, 279
input text boxes this, 56, 225
asterisk character password display, 22
backgrounds, 22
borders, 22
308
43657-5 Index.F 1/31/02 12:28 PM Page 309
FLASH ACTIONSCRIPT:
Your visual blueprint for
creating Flash-enhanced Web sites
L loops
compound assignment operators, 130–131
labels, frames, 31, 48 conditional, 140–141
Lasso tool, object selection, 6–7 counters, 122
Layer Properties dialog box, 5 for action, 142–143
layers, 2, 4–5, 19, 30–31 Go To action, 49
Leap Year movie playback, 34
date validation, 246–247 sounds, 16–17
modulo (%) operator determination, 120 LOW quality, movie clips, 86–87
occurrences, 247
Learning Interactions, Knowledge Tracks, 243 M
lengths, array, 166–167
Macintosh
less than (<) operator, 126
Controller Toolbar access, 39
less than or equal to (<=) operator, 127
FlashPlayer file, 54
lettercase, input value, 244
margins, static text boxes, 21
levels
mask layer, 4–5
defined, 226
masked layer, create, 4–5
depth, 154–155
mathematical calculations
movie clips, quality, 86–87
convert a string to a number, 214–215
movies, specification, 228
error evaluation, 216–217
Library
numeric values, 196–197
bitmap fill, 7
raise a power, 193
instances, create, 10–11
round numbers, 194
movie clips, sharing, 8
square root, 193
sounds, imported, 16
mathematical equations, expressions, 37
store panels, 259
mathematical functions, 192
symbols, automatic storage, 8–9
mathematical operators, data types, 88
versus common library, 237
MEDIUM quality, movie clips, 86–87
Line tool, 6
menus, 53
line weight, stroke, 7
messages, 55, 62
lines, 6–7
methods
linkages, movie clips, attachment, 148–149
acos, 192
links, Web pages, opening, 53
asin, 192
List Objects, test environment, 272
atan, 192
List Variables, test environment, 273
attachMovie, 148–151, 234
literal values, quote (“) character, 36–37
attachSound, 202
load
concat, 168
movie clips, 156–157, 226–229
cos, 192, 262
movies, 226–229
dot syntax, 224–225
variables, 94–95
duplicateMovie, 234
local variables, 88
getAscii, 198, 200
lock, layers, 4–5
getBeginIndex, 208
Lock/Unlock All Layers column, 5
getbounds, 152–153
Lock/Unlock icon, 5
getBytesLoaded, 156–157
logarithms, mathematical functions, 192
getBytesTotal, 156–157
logical operators, 132–133
getCode, 198
getDay, 184
309
43657-5 Index.F 1/31/02 12:28 PM Page 310
INDEX
getEndIndex, 208 Selection.getFocus, 208, 246
getFullYear, 182–183 Selection.setSelcection, 246, 250
getHours, 184 setDate, 188–189
getMilliseconds, 184 setFullYear, 188
getMinutes, 184 setHours, 190
getMonth, 182–183 setMilliseconds, 190
getPan, 206 setMinutes, 190
getRGB, 176–177 setMonth, 188
getSeconds, 184 setRGB, 176
getTimezoneOffset, 185 setSeconds, 190
getUTCDate, 182–183 setTransform, 178
getUTCDay, 184 setUTCDate, 188
getUTCFullYear, 182–183 setUTCFullYear,, 188
getUTCHours, 184 setUTCHours, 190
getUTCMilliseconds, 184 setUTCMilliseconds, 190
getUTCminutes, 184 setUTCMinutes, 190
getUTCMonth, 182–183 setUTCMonth, 188
getUTCSeconds, 184 setUTCSeconds, 190
getYear, 183 setVolume, 206
globalToLocal, 160–161 setYear, 189
hitTest, 158–159 shift, 170–171
join, 174 sin, 192, 262
Key, 260 slash syntax, 224–225
Key.isDown, 200 slice, 99, 172–173
Key.isToggled, 200 splice, 170–171
localToGlobal, 160–161 start, 204
log, 192 string.charAt, 96
Math.abs, 196 string.concat, 100
Math.cell, 194 string.fromCharCode, 97
Math.floor, 194 string.indexOf, 104, 248, 250
Math.max, 196–197 string.lastIndexOf, 105, 251
Math.min*mf, 196–197 String length, 244
Math.pow, 193 string.substring, 97
Math.random, 195 string.toLowerCase, 106, 249–250
Math.round, 194 string.toUpperCase, 106, 250
Math.sqrt, 193 substring, 244
Mouse.hide, 180 swapDepths, 154–155
Mouse.show, 180 tangent, 192
MovieClip object, 146–147 toString, 174–175
new Color, 256 unshift, 168
play, 46 minus (-) operator, numeric values, subtracting, 114–115
pop, 170 mixer, colors, creating, 7
push, 168 modems, speed selection, 40
reverse, 172 modes
Selection.getBeginIndex, 208 Actions panel, 24–27
Selection.getCaretIndex, 208 symbol-editing, 9, 19
Selection.getEndIndex, 208
310
43657-5 Index.F 1/31/02 12:28 PM Page 311
FLASH ACTIONSCRIPT:
Your visual blueprint for
creating Flash-enhanced Web sites
311
43657-5 Index.F 1/31/02 12:28 PM Page 312
INDEX
MovieClip object, methods, 146–147 stop action, 44
movies stop playback, 39
antialiasing, 50–51 streaming, 41
boundary, 33 supported file formats, 32
comments, adding, 42–43 SWF extension, 32, 39
compression, 34 targeting, 46
crop, 33 test environment, 39–41
Debugger testing, 38–39, 245, 271
activation, 32 transparency, adjustments, 68–69
troubleshooting, 269 unload, 226–229
device fonts, 34 width adjustment, 33, 65
display list, 222–223 zoom in/out, 41
display quality, 41 multiline comments, 42–43
display variables, 273 multiline dynamic text box, 23
elapsed time, 212 multiline input text box, 22
FLA extension, 32, 39 multiplication (*) operator, 88, 116–117
Flash Player, 54–55 multiply, numeric values, 116–117
fonts, embedded, 20
height adjustments, 65 N
hierarchy, 222–223 names
HTML document templates, 34 instance, 66–67
ignore trace actions, 32 layers, 4–5
JPEG graphics quality adjustments, 32 movie, 32
load, 226–229 movie clips
load order, 32 attached, 150–151
location, adjustments, 65 instance, 19
loop playback, 34 scenes, 3
loops, continuous playback, 38 symbols, 9
modems, speed selection, 40 tweens, 14
names, 32 variables, conventions, 88–89
one-frame, 252 Netscape Navigator, embed tag, 33
Output Window, syntax errors, 270–271 non-numeric (NaN) values, 196
pause, 34 normal layer, create, 4–5
play action, 46–47 Normal mode, 24–27, 48–49
playback, 34 NOT (!) operator, 132–133
playhead, positioning, 48–49 Num Lock key, Status bar display, 41
preloader animations, 62 NUM, appended actions, 280
preloaders, 156 numbers
print, 60–61 array element, 162–163
publish, 32–35 base ten, 215
rewind, 39 base two, 215
scenes, testing current, 38–39 conversions, 108–109
scripts, debug, 274–275 data type, 88
shortcut menu, 34 floating-point, 119
size report, 32 integer, 119
smart clips, custom interface, 240–243 random, generating, 195
sounds, 33–34, 38–39
312
43657-5 Index.F 1/31/02 12:28 PM Page 313
FLASH ACTIONSCRIPT:
Your visual blueprint for
creating Flash-enhanced Web sites
313
43657-5 Index.F 1/31/02 12:28 PM Page 314
INDEX
subtraction (-), 88 play, movie, 46–47
ternary, 110 playback
unary, 110 movies, 34
operators reference, 284–285 sounds, 16, 204–205
Operators statement, 24 playhead, 2, 48–49
Options menu, Output Window, 270–271 plus (+) operator, numeric values, adding, 112–113
OR (||) operator, 132–133 plus sign (+) character
order addition operator, 88, 100
layers, changing, 4–5 numeric values, adding, 112–113
scenes, changing, 3 PNG graphics, movie support, 32
outline view, layers, 5 pointers, mouse, 180–181
outlines, graphic stroke lines, 6–7 post-decrement (—) operator, 124–125
Outlines icon, 5 post-increment (++) operator, 122–123
Output Window, 270–273 power, raise, 193
Oval tool, 6 precedence, operators, 110–111, 117
Over state, buttons, 12–13 predefined identifiers, colors, 27
pre-increment (++) operator, 122–123
P preloader animations, 62
padlock icon, locked layer indicator, 4–5 preloader movies, 156
panels primitive data types, 92
Actions, 24–27 print, 27, 60–61
change size, 261 projectors, Flash Player, 54–55
create, 258–261 properties
described, 258 color transform, 179
emulate, 258–261 colors, 27
Frame Actions, 31 continuous update statement, 65
hiding, 260 Debugger, changing, 268–269
keys versus button use, 260 equal sign (=) value assignment, 65
Parameters, 24–25 Key object, 201
Scene, opening, 3 movie clip, 64–65
Text Options, 20–23 retrieving, 210–211
pans, sound volume, 206–207 special effects, 254–255
parameter values, add/remove from smart clips, 236–239 objects, 145
Parameters panel, statements, arguments, 24–25 retrieve, 210–211
parameters, 51, 178, 228 symbols, 8–11
parent movie clips, child placement, 234 variables, 37
parentheses (and) characters, arguments, 36 properties reference, 287
passwords, 22 Properties statement, 24
paste, frames into movie clip, 19 Properties tab, Debugger, movie clip, 268
path names, importance of accuracy, 259 Publish Settings dialog box, 16, 32–35, 50
pause, movies, 34 publish, movies, 32–35
Pen tool, 6
pencil icon, active layer indicator, 4 Q
Pencil tool, 6 quality, movie clips, levels, 86–87
percent sign (%) character, modulo operator, 88, 120–121 queries, system clock, 183
period (.) character, floating point number decimal question mark (?) character, conditional operator, 110
point, 119 QuickTime animations, movie support, 32
314
43657-5 Index.F 1/31/02 12:28 PM Page 315
FLASH ACTIONSCRIPT:
Your visual blueprint for
creating Flash-enhanced Web sites
quote (“) characters, literal values, 36–37 continuous scroll button, 219
quoted strings, colors, 27 debug, 274–275
develop in parts, 274
R document with code, 274
random numbers, generate, 195 draggable objects, 59
read-only values, 268 error message, 217
Rectangle tool, 6 fonts, sizing, 27
reference data types, 92 mouse
registration point custom pointer, 181
rotation angle change, 254 retrieving coordinates, 161
symbols, 73 movie clips
relative target path, 224–225 create design, 263, 265
remainder, modulo operator (%), 120–121 depth swap, 155
remove, scenes, 3 draggable duplicate, 235
rename, scenes, 3 duplicating, 235
reports, movie size, 32 height adjustment, 77
retrieve instance scaling, 81
coordinates, 160–161 move up/down with key press, 85
mouse coordinates, 161 moving across the Stage, 83
movies, elapsed time, 212 placing attached movie clip on Stage, 153
properties, 210–211 placing multiple instances on Stage, 151
reverse, arrays, 172–173 property adjustments, 232
rollover menu, create with getURL action, 53 replacing, 227
rotation restarting, 47
effects, create, 254–255 stopping, 45
movie clips, 72–73 toggle size, 211
registration point, 73 width adjustment, 75
round handle, 20 movies, length checking, 103
round, numbers, 194 music, toggle on/off, 203
rounded corners, Rectangle tool, 6 naming conventions, 274
rules, variables, naming conventions, 89 numbers, raise a power, 193
numeric values
S comparing two values, 197
Scale, movie clips, 78–81 discount subtraction, 115
Scene panel, 3 subtotal calculation, 113
scenes, 3, 38–39, 48 objects
scope, variables, 88 change color, 257
scripts collision detection, 159
age, calculation base on date entered, 187 post-decrement (—) operator, 125
array access [ ] operator, 163 post-increment operator, 123
arrays, removing elements, 171 preloader, 63
ASCII code retrieval, 97 printing, 27
buttons, rotate continuously, 255 smiley face, moving, 199
CGI, 228–229 sounds
colored syntax use, 274 increasing volume, 205
concatenate strings, 101 volume panning, 207
continuous action, 253 string search, 27
315
43657-5 Index.F 1/31/02 12:28 PM Page 316
INDEX
strings slash syntax, target path, 224–225
last substring occurrence search, 105 smart clips
lowercase to uppercase conversion, 106 create, 236–239
syntax check, 274 custom interface, 239–243
syntax errors, checking, 27 described, 18
text, selections, 209 Learning Interactions Library, 243
use meaningful names for parts, 274 panels, 259
visible/invisible movie clips, 71 parameters, defined, 243
scroll bars, draggable objects, 57 Smart Clips icon, 237
scrollable text box, create, 218–219 smile face, moving, 199
search smooth curves, Pen tool, 6
strings, 27, 104–105 smooth lines, Pencil tool, 6
text box, 248–251 Sound Panel, effects, 16
select Sound Properties dialog box, 16
enabled button, 13 Sound Setting dialog box, 33–34
frames, 19 sounds
instances, 11 add, 16–17
keyframes, 14 compression, 16, 34
layers, 19 create, 202–203
Stage objects, 6–7, 9 draggable objects, 57
statements, 24 event, 16
semi-colon (;) character, statements, 36–37 Library, imported sound storage, 16
set loops, 16–17
date, 186–187 movies, settings, 33–34
date values, 188–189 mute, 38–39
movie clip quality, 86–87 panning, 206–207
movie quality, 50–51 playback, 16, 204–205
volume and panning, 206–207 Sound Panel, effects, 16
shape, tweened animation type, 14 stream, 16
Shift key synchronization types, 16
draw, graphics, 6 volume, setting, 206–207
frame block selection, 19 spaces, static text boxes, 21
shortcut menu, movies, 34 special effects, trigonometric functions to create, 266–267
Show Deprecated Syntax, 27 splitter bar, Toolbox list, sizing, 26
Show Layers as Outlines column, 5 spotlight effects, mask layers, 4
Show Library icon, 9, 13 square handle, 20
Show/Hide All Layers column, 5 square root, 193
Show/Hide icon, 5 squares, Rectangle tool, 6
single line dynamic text box, 23 Stage
single line input text box, 22 coloring book, color selection, 256
single line text boxes, 20–21 Debugger, show changes, 268
single-line comments, 42–43 Hand tool, moving the Stage, 7
size report, movies, 32 instance of a symbol, 11
slash (/) asterisk (*) characters, comments, 42 interface element, 2
slash (/) character layers, making active, 4
division operator, 88
numeric values, dividing, 118–119
316
43657-5 Index.F 1/31/02 12:28 PM Page 317
FLASH ACTIONSCRIPT:
Your visual blueprint for
creating Flash-enhanced Web sites
317
43657-5 Index.F 1/31/02 12:28 PM Page 318
INDEX
scripts, 274–275 tools, 6–7
Status bar, hide/display, 41 trace actions, ignore, 32
streaming bar, 41–42 trace action, evaluate expression, 270
validate, dates, 246–247 transform, color values, 178–179
text transparency, movies/movie clips, 68–69
assigning to variable, 248 trigger actions, 252
dynamic, variables, 88 trigonometric functions, 192, 262–265
layout, 21 trigonometric special effects, create, 266–267
make selectable, 21 troubleshoot
selections, 208–209 Debugger, 268–269
text boxes Output Window, 270–271
associate variable, 248 tweened animations, 14
dates, displaying, 191 tweened frames, 14
dynamic, 23 tweens
input, 22 fading effect, 69
scrollable, 218–219 motion, 4
search, 248–251 objects, scaling, 79
search from bottom, 251 start/end frames, 14
static, 20–21
text selections, 208–209 U
text editors, ActionScript, writing, 25 unary operators, operands, 110
text files, URL encode, 94 unhide
text formats, Text Option panel, 20 layers, 4–5
Text Options panel, 20, 22–23 toolbox, 7
Text tool, 88–89 Universal Coordinated Time, 182–191
time zones, offsets, 185 unload, movie clips, 226–229
Timeline unlock, layers, 4–5
buttons, 8–9 Up state, buttons, 12–13
comments, adding, 42–43 URL encode, text files, 94
four-frame buttons, 12 URL encoding characters, 278–279
frames, preloader animations, 62 use, Actions panel, 24–27
functions, calling, 220 user input, 22, 89
interface element, 2 users, 56–59, 244
movie clips, selecting, 18
play action, 46 V
stop action, 44 validate
target path, 224–225 dates, 246–247
target requirements, 231 strings, 244–245
tweens, 15 values
times arrays, comma (,) character separator, 92
movies, elapsed time, 212 arrays, elements, 164–165
time zones, offsets, 185 ASCII, 198–200
values, setting, 190–191 Boolean variables, 90
tint, instances, 10–11 Boolean, false, 56
Toolbox, 2, 7 colors, hexadecimal, 176–177
Toolbox list, 24, 26–27 colors, transform, 178–179
318
43657-5 Index.F 1/31/02 12:28 PM Page 319
FLASH ACTIONSCRIPT:
Your visual blueprint for
creating Flash-enhanced Web sites
319
53657-5 BOB.F 1/31/02 12:28 PM Page 320
“Apply It” and “Extra” provide ready-to-run code and useful tips.
OD R D M
U M E IRE S F O
P RR EMS S
TM
TRADE & INDIVIDUAL ORDERS EDUCATIONAL ORDERS & DISCOUNTS CORPORATE ORDERS FOR VISUAL SERIES
Phone: (800) 762-2974 Phone: (800) 434-2086 Phone: (800) 469-6616
or (317) 572-3993 (8:30 a.m.–5:00 p.m., CST, weekdays) (8 a.m.–5 p.m., EST, weekdays)
(8 a.m.–6 p.m., CST, weekdays) FAX : (317) 572-4005 FAX : (905) 890-9434
FAX : (800) 550-2747
or (317) 572-4002
Qty ISBN Title Price Total
Subtotal ______________________
• • • • • • • • • • • • • • • • • • • • • • •
Shipping & Handling Charges
Each CA residents add
Description First book add’l. book Total applicable sales tax ___________________
IN, MA and MD
Domestic Normal $4.50 $1.50 $ residents add
Two Day Air $8.50 $2.50 $ 5% sales tax ______________________
Overnight $18.00 $3.00 $
IL residents add
6.25% sales tax __________________________
International Surface $8.00 $8.00 $
Airmail $16.00 $16.00 $ RI residents add
7% sales tax __________________________
DHL Air $17.00 $17.00 $
TX residents add
8.25% sales tax __________________________
Name____________________________________________________________________________________________
Total ______________________
Address __________________________________________________________________________________________
Company ________________________________________________________________________________________
City/State/Zip _____________________________________________________________________________________
Daytime Phone____________________________________________________________________________________