Chapter 15. Controlling the Timeline and Animation

Ordinarily, Flash assumes you want to play your animation in sequential order from the first frame in your timeline to the last. But sometimes "start at the beginning and quit at the end" isn't exactly what you want. Fortunately, by using a combination of scenes, frame labels, and ActionScript (Chapter 12),you can control your animation virtually any way you like.

For example, say you're putting together an instructional animation. You want to start with an introductory section, move on to the meat of your topic, and then wrap up with a question-and-answer section. If you organize these sections into separately named scenes, then you can play with the order of your animation quickly and easily. If you decide to reposition the question-and-answer scene directly after the introduction as a kind of pretest, for example, you can do that with a simple drag of your mouse. You can even add buttons that the trainee can click to replay the question-and-answer scene over and over, as many times as she likes.

In this chapter, you'll see how to stop and start playback using ActionScript code. You'll see how to use Flash labels, scenes, and ActionScript to make the most common types of nonsequential playback effects, including looping (replaying a section of your animation over and over again). To make these effects easy to test, you'll also see how to add interactive buttons to your animations. To start it off, this chapter describes how to control the overall speed at which Flash plays your animation on your audience's computers.

As you saw in Chapter 1, animations are nothing more than a series of content-containing frames that Flash plays one after another so quickly that your eyes interpret the overall effect as continuous movement.

You get a pretty good idea of how it will appear to your audience when you test it on your own computer. But the speed at which Flash actually displays your frames on someone else's computer depends on several factors, many of which you can't control:

The easiest factor to control—and the only one covered in this chapter—is the last one: the frame rate. The following section shows you how to set a new frame rate for an animation.

When you create a new animation, Flash assumes a maximum frame rate of 24 fps (frames per second). In other words, given the constraints listed in the previous section, Flash tries its best to display one frame every 1/24th of a second. Here's another way to look at frame rate: If your animation spans 240 frames, you're looking at roughly 10 seconds of screen time.

In many cases, the standard 24 fps works just fine, but if you're planning to put your animation up on a website, you may want to consider a slower rate, like 12 fps. Your rule of thumb should be to use the lowest frame rate that still provides smooth action for your animation. Usually, you decide on a frame rate when you create a new Flash document. But you can change the frame rate at any time using the document properties (as shown in Figure 15-1) or using ActionScript. Changing the frame rate has some pretty significant side effects. First and most importantly, higher frame rates speed up your document. If you have a 4-minute animation with a 12 fps frame rate and you change it to 24 fps, your animation now finishes in 2 minutes. Too high a frame rate can make animation look blurry. Too slow a frame rate creates herky-jerky movements and may cause audio syncing problems.

The next few examples use a Flash document, 15-1_Frame_Rate.fla, that you can find on the Missing CD page at www.missingmanuals.com/cds.

To see the effects of different frame rates, follow these steps and feel free to add some of your own experiments:

  1. Open 15-1_Frame_Rate.fla, and then press Ctrl+Enter (⌘-Return on a Mac) to test the animation.

    The Stutz Bearcat auto races across your screen at a blazing 12 frames per second (fps). The car is the only animated object in the movie and uses a motion tween to move from left to right across the screen.

  2. Below the timeline, click the Frame Rate setting (fps), and then type 8 (Figure 15-2).

    You can click the Frame Rate and then type a new number, or you can drag to change the setting.

  3. Test the animation by pressing Ctrl+Enter (⌘-Return).

    The car moves across the screen in a decidedly herky-jerky fashion.

  4. Click Modify→Document to open the Document Properties box, as shown in Figure 15-1, and then for Frame rate, type a number from 24 to 120. Click OK.

    Flash lets you type in a frame rate of anything between 0.01 and 120. But in most cases, you want to stick with a frame rate of somewhere between 12 and 24 (the standard Hollywood movie frame rate). Every animation is different, of course, and you might actually want to create an unusual effect. The Document Properties window disappears. In the timeline, you see the new frame rate.

  5. Test your new frame rate by choosing Control→Test Movie.

    If you type in a frame rate greater than 24, the car moves noticeably faster and may even look a bit blurry.

ActionScript gives you the tools to change the frame rate for your animation, too, since the stage has a frameRate property. (If you need more basics on ActionScript and properties, see Chapter 12.) When you use ActionScript to set the frameRate property for the stage, it has the same effect as setting a new frame rate in the timeline when you're designing your animation.

  1. Add a new layer for actions to the timeline.

    If you're going to add code to the timeline, it's good practice to add a special layer at the top just for code and to name it something like "actions." That way, it's much easier later if you have to hunt down and debug your code.

  2. In the actions layer, click the first frame, and then open the Actions panel (Window→Actions).

    The Actions panel opens, as shown in Figure 15-3. For more details about the tools available in the Actions panel, see Writing ActionScript Code in the Timeline.

  3. In the Actions panel, type the following line of code:

    stage.frameRate=60;

    Even though you're changing the frameRate in ActionScript, the rate showing below the time line still shows a frame rate of 24 fps (or whatever you used in the last setting). In fact, the frameRate doesn't officially change until the actions on Frame 1 of the timeline are processed.

  4. Click the 22nd frame in the actions layer of the timeline, and then press F7.

    Flash inserts a blank keyframe in the timeline.

  5. In the Actions panel, type the following code:

    stage.frameRate=6;

    Make sure that the 25th frame in the timeline is selected when you type in the code, because you want the frame rate to change at that point in the animation. The Current Selection tab should change to show a frame symbol and the words actions : Frame 22.

  6. Test your animation using Ctrl+Enter (⌘-Return on a Mac).

    The Stutz Bearcat races up to the stop sign and performs a Hollywood stop, slowing down and rolling through the intersection, and then off the stage.