Your First Five Actions


Now that you have a general picture of what actions do, let's look at five common actions in detail. At this point, we're describing only the functionality of each action, not how to add an action to your movie. We provide information on adding an action in the next section, "Making Actions Happen with Event Handlers."

Cross-Reference 

You will find coverage of further actions (and full-blown ActionScript) in later chapters.

Actions in the Flash interface appear alphabetically sorted from top to bottom. In the following sections, complementary actions are grouped together.

gotoAndPlay and gotoAndStop

These "go to" actions change the current frame of the movie to the target frame specified as the action's parameter. There are two variations:

  • gotoAndPlay: Changes the current frame to the frame specified, and then executes a play action. gotoAndPlay provides the capability to show animated sequences as preludes to individual content areas. gotoAndPlay also gets frequent use in choose-your-own-adventure style animations, in which the user guides an animated character through different paths in a narrative.

    Note 

    If you use a gotoAndPlay() action to go to a frame that has a stop() action, the timeline will stop at the new frame.

  • gotoAndStop: Changes the current frame to the frame specified and then halts play-back on that frame. gotoAndStop is often used to produce toolbar-style interfaces where the user clicks buttons to view different areas of content in a movie.

Both actions enable you to jump to certain areas of the Flash movie. The parameters of these actions start with the largest time unit, the scene, and end with the smallest one, the frame.

You can specify frames in other scenes as the target of goto actions with the scene parameter. As shown in the following code, you type the name of the scene first, with double quotes around the name, and then the frame number (without quotes) or frame label. In this example, "Scene 2" is the name of the scene and "animate_in" is the frame label in that scene to jump to.

 gotoAndPlay("Scene 2", "animate_in"); 

However, if you only specify one parameter in a gotoAndPlay or gotoAndStop action, the parameter is interpreted as a frame label. For example, the following code tells the current timeline to jump to its "menu" frame label:

 gotoAndStop("menu"); 

Caution 

While we haven't looked at actions specifically for use in Movie Clip instances, make a note that you can't use a goto action specifying a scene within a Movie Clip instance. In this case, you should target the Main Timeline to go to and stop (or play) the keyframe label in the desired scene, omitting the Scene's name. For example, _root.gotoAndStop("products"); executed from a Movie Clip Timeline would tell the Main Timeline to go to and stop on the frame label products, which would be located in a different scene.

There are three methods of specifying the frame to which the movie should go when it receives a goto action. The methods for specifying the frame are

  • number: Specifies the target frame as a number. Frame 1 is the beginning of the movie or scene. Frame numbers span scenes, so if you have a movie with two scenes, each containing 25 frames, and you add a goto action with Frame Number set to 50, your action advances the movie to the 25th frame of the second scene. Frame numbers should not use surrounding quotes, as frame labels do. The following action tells the current timeline to jump to frame 10 and start playing:

     gotoAndPlay(10); 

    Caution 

    Using frame numbers to specify the targets of goto actions can lead to serious scalability problems in Flash movies. Adding frames at the beginning or in the middle of a movie's Timeline causes the subsequent frames to be renumbered. When those frames are renumbered, all goto actions that use frame numbers must be revised to point to the correct new number of their target frames.

    In the vast majority of cases, goto actions that use a frame label to specify target frames are preferable to goto actions that use a frame number to specify target frames. Unlike numbered frame targets, goto actions with labeled frame targets continue to function properly, even if the targeted frame changes position on the timeline.

  • label: Individual keyframes can be given names via the Label field in the Property inspector. Once a frame is labeled, a goto action can target it by name. To specify a label as the target of a goto action, type the name of the frame as the action's parameter. The following example tells the Flash movie to go to the frame labeled "products" and stop at that frame.

     gotoAndStop("products"); 

  • ActionScript expression: Specifies the target frame as an interpreted ActionScript code segment. You use expressions to dynamically assign targets of goto actions. Here's a quick example of a string variable being used as a frame label in ActionScript 1.0 style:

     var targetLabel = "products"; gotoAndPlay(targetLabel); 

    Notice that the term targetLabel does not use quotes, because it is not the actual frame label name. When the Flash Player interprets this action, it looks up the value of targetLabel, which is "products", and inserts that value into the gotoAndPlay action. In ActionScript 2.0, the same action looks like this:

     var targetLabel:String = "products"; gotoAndPlay(targetLabel); 

Cross-Reference 

Expressions are covered in Chapter 24, "Knowing the Nuts and Bolts of Code."

nextFrame and prevFrame

The nextFrame and prevFrame actions act like a gotoAndStop action, in that they both transport the timeline to a new position and stop.

  • nextFrame: This action tells the current timeline to move forward one frame and stop playback. nextFrame can be used in conjunction with prevFrame to quickly set up a slide-show-style walkthrough of content, where each of a series of contiguous keyframes contains the content of one "slide." This action does not use any parameters. The following code moves the timeline to the next frame:

     nextFrame(); 

  • prevFrame: This action moves the current timeline backward one frame and stops playback. For example, if the timeline is on frame 20, and the movie runs a prevFrame() action, the timeline moves to frame 19. As with the nextFrame action, prevFrame does not use any parameters:

     prevFrame(); 

nextScene and prevScene

These actions advance the Flash movie to a new scene. Here's how they work:

  • nextScene: This action tells the current timeline to move to the first frame of the next scene. You can use nextScene for more elaborate slide shows or demonstration movies, where each scene contains animated content with a stop action on the last frame. The last frame of the scene then contains a button using the nextScene action. This action does not use any parameters. The following code tells the movie to jump to the next scene:

     nextScene(); 

  • prevScene: This action jumps the movie to the previous scene. For example, if the playhead is in Scene 2, the timeline moves to the first frame of Scene 1. As with the nextScene() action, prevScene() does not use any parameters:

     prevFrame(); 

Note 

The nextScene and prevScene actions do not automatically recycle the scenes when the last or first scene is reached, respectively. For example, if you have three scenes and use a nextScene action while the movie is on the last scene, the movie will not jump back to the first scene.

Tip 

While you may find it simpler to segment your Flash content across several scenes as you begin to learn Flash, most seasoned Flash designers and developers only use one scene, and separate content across several Movie Clip symbols placed on one or more frames of Scene 1. Scenes are not compatible with standard targeting syntax, as you'll learn in the next chapter.

On the CD-ROM 

You can find an example of nextScene and prevScene usage in the document named nextScene.fla in the ch18 folder of this book's CD-ROM.

Play and Stop

These simple actions are the true foundations of Flash timeline control. play sets a movie or a Movie Clip instance in motion. When a play action is executed, Flash starts the sequential display of each frame's contents along the current timeline.

The rate at which the frames are displayed is measured as frames per second, or fps. The fps rate can be set from 0.01 to 120 (meaning that the play action can cause the display of as little as 1 frame every 100 seconds to as many as 120 frames in 1 second, subject to the limitations of the computer's processing speed). The default fps is 12.

Once play has started, frames continue to be displayed one after the other, until another action interrupts the flow, or the end of the movie or Movie Clip's timeline is reached. If the end of a movie's timeline is reached, the movie either loops (begins playing again at frame 1, Scene 1), or stops on the last frame.

Once the end of the Movie Clip's timeline is reached, playback loops back to the beginning of the clip, and the clip continues playing. To prevent looping, add a stop action to the last frame of your Movie Clip.

Note 

A single play action affects only a single timeline, whether that timeline is the main movie timeline or the timeline of a Movie Clip instance. For example, a play action executed inside a Movie Clip does not cause the Main Timeline to begin playing. Likewise, any goto action on the Main Timeline doesn't migrate to the Movie Clips that reside there. A timeline must be specifically targeted to control playback along that timeline. If there is no specified target, the action is referring to its own timeline. However, this is not the case for animations within Graphic symbol instances. An animation in a Graphic symbol is controlled by actions on the timeline in which the symbol instance is present — Flash ignores actions on a Graphic symbol's timeline.

stop, as you may have guessed, halts the progression of a movie or Movie Clip that is in a play state. stop is often used with buttons for user-controlled playback of a movie, or on frames to end an animated sequence.

Tip 

Movie Clip instances placed on any timeline begin to play automatically. Remember to add a stop action on the first frame of a Movie Clip if you don't want it to play right away.

stopAllSounds

A simple but powerful action that mutes any sounds playing in the movie at the time the action is executed, stopAllSounds does not disable sounds permanently — it simply cancels any sounds that happen to be currently playing. It is sometimes used as a quick-and-dirty method for making buttons that shut off background looping soundtracks. stopAllSounds is not appropriate for controlling whether individual (or specific) sounds are played or muted.

getURL

Want to link to a Web page from a Flash movie? No problem. That's what getURL is for. You can find the getURL action in the Global Functions ð Browser/Network booklet of the Actions panel. getURL is simply Flash's method of making a conventional hypertext link. It's nearly the equivalent of an anchor tag in HTML (<a href="...">), except that Flash's getURL can also send variables for form submission. getURL can be used to link to a standard Web page, an FTP site, another Flash movie, an executable, a server-side script, or anything that exists on the Internet or on an accessible local file system.

getURL has three parameters that are familiar to Web builders (the first one, url, is required for this action to work):

  • url: This is the network address of the page, file, script, or resource to which you are linking. Any value is permitted (including ActionScript expressions), but the linked item can be displayed only if the reference to it is correct. url is directly analogous to the HREF attribute of an HTML anchor tag. You can use a relative or absolute URL as well. Examples:

     http://www.yoursite.com/ ftp://ftp.yoursite.com/pub/documents.zip menu.html /cgi-bin/processform.cgi /script/form.cfm 

    Since Flash 4, getURL can be used to link to documents on the Web from the stand-alone Flash player. Execution of a getURL action in the stand-alone player causes the default Web browser to launch and load the requested URL.

    Tip 

    You can specify secure domain URLs by using the https protocol for SSL (Secure Socket Layer) connections.

  • window: This is the name of the frame or window in which you wish to load the resource specified in the url setting. The window parameter is directly analogous to the target attribute of an HTML anchor tag. In addition to enabling the entry of custom frame and window names, the window parameter can use the following browser-standard target names:

    • "_self": Loads the URL into the same frame or window as the current movie. If you do not specify a window parameter in the getURL action, this behavior will be the default.

    • "_blank": Creates a new browser window and loads the URL into it.

    • "_parent": Removes the current frameset and loads the URL in its place. Use this option if you have multiple nested framesets, and you want your linked URL to replace only the frameset in which your movie resides.

    • "_top": Loads the URL into the current browser and removes all framesets in the process. Use this option if your movie is in a frame, but you want your linked URL to be loaded normally into the browser, outside the confines of any frames.

    Note 

    Frame windows and/or JavaScript windows can be assigned names. You can target these names by manually typing the name in the Window field. For example, if you had a frame defined as <frame name=" main". . .>, you could load specific URLs into a frame named main from a Flash movie.

  • method: This parameter enables getURL to function similarly to an HTML form submission. For normal links, the method parameter should be omitted. But in order to submit values to a server-side script, one of the submission methods, "GET" or "POST", should be specified. For a complete discussion on submitting data to a server from a Flash movie (using the new LoadVars object), see Chapter 29, "Sending Data In and Out of Flash."

Tip 

getURL functions in the Test Movie environment. Both the Flash stand-alone player and the Test Movie command give you access to external and/or local URLs.

Let's look at some quick examples of how you can write a getURL action. The following code tells the browser to load the URL, www.wiley.com, into the current browser window:

 getURL("http://www.wiley.com"); 

Alternatively, you can specify a unique target for the loaded URL. The following example loads an HTML document named menu.html into a frame named menu_frame:

 getURL("menu.html", "menu_frame"); 

A more advanced usage of the getURL action sends variables from the Flash movie to a Web server's script, which is set up to receive the variables. The following code looks up the version of the Flash Player playing the movie and sends to a script that logs the information:

 var playerVersion = getVersion(); getURL("http://www.mysite.com/scripts/log.cfm","_self","GET"); 

As we mentioned with the goto actions, you can also use expressions with getURL actions. Expressions can be used as parameters of any ActionScript action. The following example uses a string variable to specify the URL used by a getURL action:

 var siteURL = "http://www.flashsupport.com"; getURL(siteURL); 

You should start familiarizing yourself with the ActionScript notation that Flash uses for each action (see Table 18-2). As you use Flash for more advanced interactivity, you'll need to have a firm grasp of code notation. Part VII, "Approaching ActionScript," teaches you how to start building more advanced code.

Table 18-2: Common Actions and ActionScript Notation

Action

ActionScript notation

Arguments

gotoAndStop

gotoAndStop(arguments);

Scene Name (Frame Label, Number, or Expression)

gotoAndPlay

gotoAndPlay(arguments);

Scene Name (Frame Label, Number, or Expression)

nextFrame

nextFrame();

None

prevFrame

prevFrame();

None

nextScene

nextScene();

None

prevScene

prevScene();

None

play

play();

None

stop

stop();

None

stopAllSounds

stopAllSounds();

None

getURL

getURL(arguments);

url, target frame or window, method for form submission




Macromedia Flash 8 Bible
Macromedia Flash8 Bible
ISBN: 0471746762
EAN: 2147483647
Year: 2006
Pages: 395

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