gotoAndPlay( ) Global Function

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 18.  ActionScript Language Reference
gotoAndPlay( ) Global Function Flash 2

move the playhead to a given frame, and play the current clip or movie
gotoAndPlay(frameNumber) gotoAndPlay(frameLabel) gotoAndPlay(scene, frameNumber) gotoAndPlay(scene, frameLabel)

Arguments

frameNumber

A positive integer indicating the number of the frame to which the playhead of the current timeline should jump before playing. If frameNumber is less than 1 or greater than the number of frames in the timeline, the playhead is sent to either the first or last frame, respectively.

frameLabel

A string indicating the label of the frame to which the playhead of the current timeline should jump before playing. If frameLabel is not found, the playhead is sent to the first frame of the current scene.

scene

An optional string indicating the name of the scene that contains the specified frameNumber or frameLabel. If not supplied, the current scene is assumed.

Description

When invoked without a scene argument, gotoAndPlay( ) sends the playhead of the current timeline to the frame specified by either frameNumber or frameLabel and then plays the timeline from that point. The "current timeline" is the timeline of the movie clip or main movie from which the gotoAndPlay( ) function is invoked.

When jumping to another frame, any intervening frames are skipped and their scripts are not executed.

If two arguments are specified in the gotoAndPlay( ) function call, the first argument is assumed to be the scene. If only one argument is specified, it is treated as a frameNumber or frameLabel, and the current scene is assumed.

When invoked with a scene argument, gotoAndPlay( ) moves the playhead to the frame number or label in the specified scene and then plays that scene. If a scene argument is used, the gotoAndPlay( ) function must be invoked from the _root timeline; otherwise, the operation fails silently and the playhead is not sent to the destination frame. Note that scenes are flattened into a single timeline during movie playback. That is, if scene 1's timeline contains 20 frames, and scene 2's timeline contains 10 frames, then we can send the playhead to frame 5 of scene 2 by placing the following code in scene 1:

gotoAndPlay(25);

I recommend against using scenes when working with ActionScript-intensive movies. Unlike movie clips, scenes are not represented by objects and cannot be manipulated directly by most built-in functions. It's normally better to use labels and movie clips as pseudo-scenes in your timeline, instead of using Flash's scene feature.

The global gotoAndPlay( ) function affects the current timeline only. The frames or state of other movie clips within the current timeline are not affected. To cause other movie clips to play, you must issue a separate play( ) or gotoAndPlay( ) command for each movie clip, including nested ones. To apply the gotoAndPlay( ) function to a clip other than the current movie clip, use the movie clip method form, MovieClip.gotoAndPlay( ).

Bugs

In Flash Player 5 and Flash Player 6, the gotoAndPlay( ) function does not work when used in an onClipEvent( ) handler with a string literal for frameLabel.

In Flash Player 4 and later, when a nonliteral frameLabel argument is specified in any scene other than the first, the playhead jumps to the specified label plus the total number of frames in all scenes before the current scene.

To work around both bugs, use the movie clip alternative, MovieClip.gotoAndPlay( ), and specify this to indicate the current clip, as in this.gotoAndPlay("myLabel"), rather than gotoAndPlay("myLabel").

Example

// Go to frame 5 of the current timeline, and play it gotoAndPlay(5); // Go to frame 10 of the exitSequence scene, and play it gotoAndPlay("exitSequence", 10); // Go to frame "goodbye" of the exitSequence scene, and play it gotoAndPlay("exitSequence", "goodbye"); // Caution! This plays the frame labeled "exitSequence" in the current scene gotoAndPlay("exitSequence"); // This plays frame 1 of the exitSequence scene gotoAndPlay("exitSequence", 1);

Reader Exercise: Write a function that recursively plays all nested movie clips within the clip from which gotoAndPlay( ) is called. (Hint: Use a for-in loop to retrieve a list of child clips nested within each clip.)

See Also

gotoAndStop( ), MovieClip.gotoAndPlay( ), play( ), stop( )



    ActionScript for Flash MX. The Definitive Guide
    ActionScript for Flash MX: The Definitive Guide, Second Edition
    ISBN: 059600396X
    EAN: 2147483647
    Year: 2002
    Pages: 780
    Authors: Colin Moock

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net