AS Reference  :  Notes Index  :  Resources  :  About/Contact  :  Downloads

Controlling the Playhead of a Flash Movie

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".

Intro
Flash: What & How
Example Sites
Create
Draw, Edit Shapes
Gradients
More Drawing Tips
Import
A Sample
Animate
Frames, Keyframes
Motion Tweens
More Motion Tweens
Shape Tweens
Masks
Control
Stop/Replay
Movieclips Intro
Movieclip Reference
Site Structure 1
Slideshow Movieclip
Contact Form
Scroll Resume
Preloader
Site Structure 2
Publish
Display Options
Player Detection
Optimize
AS 2.0 Basics
Intro to Syntax
Playhead Commands
Playhead Cmds 2
Coded Tween
onEnterFrame
Intro to Classes
Declare/Assign
Comments, Trace
Simple Data Types
Arrays & Objects
Code Blocks
Operators
Beyond Buttons
Code Structure
Toggle Controls
Group of Buttons
Drag and Hit
Distort Magnifier
Scroll Text
Bee Game
Dart Shooter
Sound Control
Easing Slider
Easing Slider 2
Components Intro
Timers & Delays
Dynamic Content
Intro
Drawing API
Create Text
Attach Movieclips
Easing Slider 3
Easing Slider 4
Load jpg/swf
Sliding Viewer
Preload swf
XML
Easing Slider 5
Server Comm
LoadVars (w/ PHP)
AS - PHP Lookup
Text File
Database 1:LoadVars
Database 2:Remoting
Read from directory
AS 2.0 Classes
Intro
Math
Key
Date
Color
EventDispatcher
New Samples
Pie Chart
Event-model Emailer
Tween Sequence
Fuse Sequence
SVG in Flash
Bitmap Topo
SWF as Data Holder
Two-level Menu
Yahoo! Flash Maps
Class-based Game
ASTB Samples
Disclaimer
3D Outlines
Bounce Collide
Address Book
Save Drawings
Home  :  Notes Index  :  Resources  :  About/Contact  :  Downloads