You can attach onClipEvent handlers to the same movie clip instance.. The first would exe-cute when the movie clip first loads or appears on the Stage; the second executes when the movie
Trang 1on(press) { trace("The button has been pressed.");
} You can specify two or more events for each on() handler, separated by commas The ActionScript in a handler executes when one
of the events specified by the handler occurs
For example, the following on() handler attached to a button will execute whenever the mouse rolls over or out of the button
on(rollOver, rollOut) { trace("mouse rolled in or out");
}
If you want different scripts to run when different events occur, you have the option to attach more than one handler to an object
You can attach onClipEvent() handlers to the same movie clip instance The first would exe-cute when the movie clip first loads (or appears on the Stage); the second executes when the movie clip is unloaded from the Stage
onClipEvent(load) { trace("loaded");
} onClipEvent (unload) { trace("unloaded");
If you’re working with ActionScript 2.0 (not
supported in ActionScript 3.0), you can attach
clip events to movie clips, which triggers an
action specified in the onClipEvent handler
Event handlers, also known as event
listen-ers, control when events in Flash occur When
you create a script, some event will be
invoked to trigger that particular action You
might want a specific movie clip to stop
play-ing when another movie clip loads on the
Stage, or when the user clicks or moves their
mouse
The Clip Event is one of a series of event
handlers that Flash uses to create actions
within a Flash movie You can attach event
handlers directly to a button or movie clip
instance by using the onClipEvent() or the
on() handlers The onClipEvent() handles
movie clip events, and on() handles button
events To use an on() or onClipEvent()
han-dler, attach it directly to an instance of a
but-ton or movie clip on the Stage, and then
specify the event you want to handle for that
instance For example, the following on()
event handler executes whenever the user
clicks the button the handler is attached to
Working with Clip Events
Trang 2For ActionScript 2.0, you can only attach an onClipEvent() to a movie clip instance that has been placed on the Stage You can't attach an onClipEvent() to a movie clip instance that is created at runtime; for example, using the attachMovie() method However, you can still attach multiple event handlers Using different event handlers within the same Flash document do not conflict with each other You could have a but-ton with an on(press) handler that tells the SWF file to play, and the same button can have an onPress method, for which you define a func-tion that tells an object on the Stage to rotate When the button is clicked, the SWF file plays, and the object will rotate Being able to consolidate different event handlers with a single instance gives you greater control, as well as less Stage clutter
Attaching a Clip Event
to a Movie Clip
Attach an onClipEvent
to a Movie Clip
Create or open a Flash document
(ActionScript 2.0), place a movie
clip on the Stage, and then select
the movie clip
Give the movie clip a unique
instance name in the Properties
panel
Move down the Timeline and add a
keyframe at frame 80
Click the Insert menu, point to
Timeline, and then click Keyframe.
Add a second movie clip to the
Stage, and then select the second
movie clip
Enter the script as shown in the
illustration
Click the Control menu, point to
Test Movie, and then click Test.
When the playhead hits frame 80 it
loads the second movie clip The
loading of the movie will trigger
the onClipEvent handler, and stop
the playing of the movie clip with
the unique instance name of
movie2
7
6
5
4
3
2
1
3 5
6 1
Trang 3Working with Loops
Loops allow Flash to perform an action
repeatedly You can use a loop to create a
dynamic drop-down menu, validate data,
search for text, duplicate movie clips, and
even detect collisions in games that have
pro-jectiles and objects Conditional statements
let you execute an action based on a specific
condition You can have a specific action
con-tinue to loop until a certain condition is met
For example, continue to search for a specific
text string until it is found or the end of the
text document is reached Loops come in two
forms—While loops and For loops While
loops wait for a specific condition to start or
stop the loop That may sound similar to the
For loop, with one exception: The For loop is
self-contained, and the While loop works with
an external condition, or one outside the
scope of the loop
◆ While Loops While loops continue to
execute while a certain condition exists
(keep looping or searching) until a
specific value is reached
i = 4;
while (var i > 0) {
my_mc.duplicateMovieClip("newMC" + i, i );
i ;
}
◆ For Loops For loops are
self-contained counters For example, loop
then stop
x = x;
for (x=0; x<=10, ++x) {
myClip.duplicateMovieClip ("myClip" + x, x);
myClip._rotation =45 + x * 10;
}
When you create a Looping action, you can further control the loop by using the fol-lowing loop exceptions:
◆ Continue The continue exception lets you stop the current loop from performing its actions and jump directly to the next cycle of the loop
◆ Break The break exception is used to exit a loop, even if the original condition that is driving the loop is still true
For example, if you create a While loop using the following script:
total = 0;
i = 0:
while (++i <=20) {
if (i == 10) { continue;
} total +=i;
} The results would be a script that executes and adds 1 to total; unless the value of i equaled 10 This would create a sequence of numbers 1 2 3 4 5 6 7 8 9 11 12 13 14 15 16 17
18 19 20 If you had used the break exception
in place of continue, the values would read:
1 2 3 4 5 6 7 8 9 Therefore, it loops whether For or While are controlled by internal or external conditions, and using a break or con-tinue exception gives you further control over the loop
Trang 4The For loop works with an increasing or decreasing numeric value For example, you could use a For loop to create several copies of a movie clip on the Stage Letting the For loop control movie clips to the Stage
is far more efficient than having to move them one at a time In addi-tion, the visitor can control when the items appear on the Stage using a button
Using For Loops
Use For Loops
Drag a movie clip from the Library
to the Stage, and then select the
movie clip
Enter a unique instance name for
the movie clip in the Properties
panel
Place a button on the Stage, and
then select the button
Enter the script (ActionScript 2.0)
as shown in the illustration
◆ ActionScript 3.0 example files
are available on the Web at
www.perspection.com
When you play the movie, clicking
on the button causes the action to
loop 10 times Each time it loops, it
duplicates the original movie clip
and rotate it by 45 degrees plus
the current value of x times 10
4
3
2
1
4 Did You Know?
You can use a For Loop to pause a
Flash movie Select a value, instruct
the loop to increment by 1, and then
loop until the value is reached Use a
loop timer for such items as a Flash
slide show, where you want the slides
to display on the stage for a given
number of seconds before moving to
the next slide
Trang 5Behaviors are time-savers because they give
you sections of ActionScript 2.0 code (not
supported in ActionScript 3.0; see Code
Snippets in the next Chapter) for common
Flash tasks Behaviors are a great way to
introduce yourself to the wonderful world of
ActionScripting without having to write all the
code For example, if you want to add a Play
ActionScript to a button, you can do it using
the Add button in the Behaviors panel, or you
can write out the code on your own; see the
example code below Using Behaviors, as
opposed to writing the code by hand, is not
better, it’s simply faster The more time you
save doing common Action-Scripting tasks
using Behaviors, the more time you will have
for the creative process
Using the Behaviors Panel
You use the Behaviors panel to apply the
behavior to a triggering object, such as a
but-ton You specify the event that triggers the
behavior, such as releasing the mouse Next
select a target object, such as the movie clip
instance, and then select settings for behavior
parameters, such as a frame number or label,
or a relative or absolute path Flash comes
with built-in behaviors, such as Load Graphic,
Duplicate Movieclip, and GotoAndPlay At
Frame Or Label To add and configure a
behavior, select a trigger object, and then step through the following general instructions (steps may vary depending on the behavior):
Click the Window menu, and then click Behaviors
Click the Add (+) button, and then select a behavior from the menu
If necessary, select settings for the behavior parameters, and then click OK
Under Event, click On Release (the default event), and then select a mouse event from the menu
4 3 2 1
Example Play ActionScript 2.0 code
on (release) {
if(this.video_1._parent._currentframe == this.video_1.parent._totalframes){
this.video_1parent.gotoAndPlay(1);
} else { this.video_1._parent.play();
Working with ActionScript Behaviors
Add button Click to select a mouse event.
Behavior parameters