Notice in the sample on the motion tweens page that after the movie plays through once, it stops at the end, and a Replay button appears, allowing the user to decide whether to play it again or not. Starting and stopping the movie can be controlled by issuing actionscript commands to tell the playhead when to stop (and/or start) playing.
Actionscript commands go either in a keyframe or on a button or movieclip.
The four main commands to control the playhead are:
| stop(); | Stop the playhead |
| play(); | Start the playhead playing from wherever it is currently |
| gotoAndPlay(n); or gotoAndPlay("somelabel"); |
Jump to frame number n (or frame label "somelabel") and start the playhead playing from there |
| gotoAndStop(n); or gotoAndStop("somelabel"); |
Jump to frame number n (or frame label "somelabel"), display that frame, and stop the playhead there |
Make the movie stop at the end
To make the sheep movie stop when it reaches the last frame, open it up and
add a new layer (actions) above all the others. Make a keyframe in the last
frame of this layer. Open the Actions Panel (choose Window, Development panels,
Actions or click the small arrow on the far right of the Properties panel.
Make sure you see "Actions - Frame" at the upper left of
the Actions panel (to be sure you're adding code to a keyframe, not a button)
and type this:
stop();
Provide a Replay button
Add another layer under the actions layer for the replay button. Put a keyframe
in its last frame (so the button doesn't show til the movie is done). To get
a copy of the Replay button, choose File, Import, Open External Library and
find buttons_forscene.fla (link at right under Files). It is a movie which just has a few buttons in it
and the command you just did should make those buttons available from its
library. (Alternately, you can get one of Flash's built-in buttons by choosing
Window, Other Panels, Common Libraries, Buttons. Find the library for Buttons.fla
and open one of its folders by double-clicking the folder icon -- you'll see
several buttons you can use).
With the last frame of the button layer selected, drag a copy of the replay button out of the buttons_forscene.fla library onto the stage of the sheep fla. Select the button, open the actions panel if it is not still open, make sure the upper left says "Actions - Button" and type this into the actions panel to tell the timeline's playhead to go to frame 1 and play:
on (release) {
gotoAndPlay(1);
}
When you put code (actionscript) on a button, it usually goes inside an "on (release) { ... }" code block, as above. The actionscript above tells Flash "when the user clicks this button and releases the mouse, send the movie's playhead to frame 1 and play from there".
Discussed on this page:
playhead commands, make movie stop at end, adding a replay button
Files:
buttons_forscene.fla
In sheepscene.zip, password required
Buttons.fla
Available in Flash MX and Flash MX 2004, access from Flash IDE as described at left
Subscription:
A password may be obtained by subscription
An access password will be emailed to the address you specify within 24 hours of receipt of payment, and will remain active for 30 days thereafter. A list of all files currently available at the site may be viewed here.