|
ActionScript: The Definitive Guide, supporting web site:
Flash Overview:
MacroMedia tutorials onFlash:
Help available from within Flash:
- HELP>USING FLASH - the basics of how to use flash
- HELP>ACTIONSCRIPT DICTIONARY- a complete reference for ActionScript
|
|
Flash interactions are controlled by a scripting
language called ActionScript. Basic interactions can be created with
a fairly simple menu-based interface, and in Flash MX 2004, a new "behavior" feature
is added to make this even simpler.
ActionScript code can go in many different places inside a flash movie,
and where it goes affects when the code is run. There are two main
kinds of places where code can be placed:
- Keyframes - ActionScript code can be attached
to any keyframe, either in the main movie timeline, or in the timeline
of a MovieClip. Scripts cannot be attached to a
normal frame - they must be put on a keyframe.
- Buttons and MovieClips - Scripts can be attached
to instances of Buttons and MovieClips on the stage. Scripts cannot be
attached to the symbols in the library, only to instances on the
stage.
|
|
You access the ActionScript capabilities by selecting the keyframe,
button or movieclip you want to make interactive, and then opening WINDOW>DEVELOPMENT
PANELS>ACTIONS (or F9). This interface enables
you to add the action you want taken by double clicking.
For many applications, we need to prevent the Flash movie from playing
through all of the pages continuously. To do this, we need to place a "stop()" action
on each of the "scene" keyframes. To do this
- select a keyframe in the timeline
- in the left side of the actions window, click on Global Functions
and then Timeline Control set of actions
- double click on the "stop" action to add
this action to the frame
- insert a button from WINDOW>OTHER PANELS>COMMON LIBRARIES>BUTTONS
on the timeline frame where you want to navigate from
- in the property inspector, give the destination keyframe a frame
label (e.g. home)
- go back to the original frame, and select the button
- in the behaviors panel, click on the "+" pull-down for MovieClip>Goto
and Stop at frame or label
- In the resulting dialog, select "_root" (because we want
to go to a new frame on the main, _root timeline), and then select
the Absolute radio button. In the "should stop playing" field,
enter the name of the frame (e.g. home) you want to go to.
- this will automatically insert the necessary ActionScript into the
button's script. You can later change this script manually, for example
to change the label to "portfolio"
|
|
It is possible to make symbols dragable by the user, so that when
they click on the symbol, they can drag it across the screen, and the
drop it when they release the mouse button.
To do this:
- create a symbol, and place it on the stage in the main timeline
- to enable dragging on mouse click, select the symbol, and in the
Behaviors panel select + and MOVIECLIP>START DRAGGING MOVIECLIP
- select the current object in the dialog, and click OK
- in the behavior panel, change the event for the start drag behavior
to ON PRESS
- with the symbol still selected, add the STOP DRAGGING MOVIECLIP
behavior, and make sure its triggering event is RELEASE
- the above will generate the following code:
- to be honest, this is not the greatest code. In particular, in
using this code you will encounter a problem if you move the mouse
too fast and release the mouse button away from the symbol. In addition,
the behavior uses an old way of specifying the symbol to be affected.
So now, edit the code to look like the this more clean code:
- in addition to allowing the user to drag a symbol, you can set
constraints on the dragging to a specified rectangle. This prevents
the user from dragging the symbol outside of an area. If you want
the dragging limited to a single line (e.g. for a sound fader knob),
then make the Left and Right the same for a vertical drag, and the
Top and Bottom the same for a horizontal drag. The way to do this
is to add what are called "parameters" to startDrag() by
putting them between the parenthesis. The parameters are as follows:
this.startDrag(lockToMouseCenter, left, top, right, bottom)
So if we wanted to make the symbol lock to the mouse center, and be constrained
to a space on the stage that was near the top left, and was a rectangle
of 100 pixels wide, by 150 pixels tall we would change the code to look
like the following:
|
|
Sometimes you want to replace the standard cursor with your own graphic.
Doing this requires putting some custom code on the graphic symbol you
want to use for the cursor.
To do this:
- select the movieClip that will be the cursor
- paste in the following code:
onClipEvent (enterFrame) {
Mouse.hide(); // hides the normal cursor
this._x = _root._xmouse; // sets the movie clip to the x mouse position
this._y = _root._ymouse; // sets the movie clip to the y mouse position
}
|
|
It is possible to create movement and animation of symbols without
using the timeline. Instead, you can use code to change the position,
rotation, or shape of a symbol. For example, if you wanted to make a
symbol spin around by itself:
- select the symbol
- paste in the following code:
onClipEvent (enterFrame) {
this._rotation = this._rotation + 5;
}
- another interesting thing to do is move the symbol across the screen:
onClipEvent (enterFrame) {
this._rotation = this._rotation + 5;
this._x = this._x + 10;
}
- and, check to see if we reached the right hand side of the screen
onClipEvent (enterFrame) {
this._rotation = this._rotation + 5;
this._x = this._x + 10;
if (this._x > 550) {// reached the right side, reset to the left side
this._x = 0;
}
}
|
|
|
To provide a taste of how scripting works before addressing the underlying
concepts, we'll create a simple script together. This script controls
the playing of an animation, so that it loops a specific number of times.
Open Flash and do the following:
- create 3 layers and name them top to bottom: Actions, Text,
Graphics
- in the graphics layer:
- create a circle and make it a symbol
- click on the frist frame in the timeline in this layer.
- in the property inspector set TWEEN to MOTION
- at frame 15, insert a keyframe (F6)
- move the circle to a new location for the end of the animation
- in the text layer:
- select the text tool, and in the property inspector select DYNAMIC
TEXT
- insert a DYNAMIC TEXT box
- in the property inspector, set the Var field to countDisplay
- at frame 15, insert a frame (F5), not a keyframe
- in the actions layer:
|
|