ActionScript 3.0 Timeline Navigation Methods

show more Using common timeline navigation methods provides you with in-depth training on Developer. Taught by Todd Perkins as part of the ActionScript 3.0 in Flash Professional CS5 Essential Training show less
please wait ...

Using common timeline navigation methods

There are many ways to navigate through a Timeline, but some are highly useful and incredibly common in ActionScript 3. On the Stage here, I have several elements. I have an animation contained in main_ mc, which if I double-click, you can preview that its Timeline is relatively simple. The bear stays stagnant for a few frames, and then he walks on the Stage, eats a fish, and walks off. How the animation is set up is irrelevant for this exercise.

We are just concerned about controlling the Timeline of that movie clip. Then I have back_btn, playPause_mc; capital P for pause, and then forward_btn. playPause_mc contains two buttons: one called play_btn and then one called pause_btn. I have already written some ActionScript code, so let's take a look at it, importing the MouseEvents and adding EventListeners to all of the buttons on the Stage.

We've already created the event handler functions, and I've also hidden the Pause button, since the movie clip is going to be stopped by default. So let's start by making the movie clip play. Inside of the playClicked function, I can type '' After play, type parenthesis and then a semicolon. So I will test the movie, and you won't see the animation taking place until I click the Play button.

After a few seconds, the bear will walk onto the Stage. So we can see that that's working. When I click the Play button, I also want to hide the Play button, and reveal the Pause button. So inside of this function, I'll type playPause, capital P, underscore mc. play_btn.visible = false. This will both hide the button and disable its interactivity.

Now I want the Pause button to show, so I'll copy this line, paste it on the next line, change play_btn to pause_btn, and change the false value to true. Now I'll test the movie, click the Play button, and you will see the Pause button shows, and of course, you will see the bear walking in. Now let's write the code for the Pause button.

Select all the code in playClicked, copy it with Command+C or Ctrl+C, click inside of the pauseClicked function, and paste the code with Command+V or Ctrl+V. In the pasted code, change play to stop, change the first false to true, and change true to false. So we want to stop the Timeline. We want to show the Play button. We want to hide the Pause button. Test the movie one more time.

Click the Play button to start the animation, and once the bear comes on, click the Pause button to watch the bear stop. So we can pause the movie, and we can click Play again to have it play; it almost got that fish. Now let's add the interactivity to skip forward and backward one frame at a time. Here what I am going to do is copy the code that shows the Play button and hides the Pause button.

This is because when I click to go to the next or the previous frame, the movie is actually going to pause anyway. So I want to treat it like I click the Pause button. So copy those two lines, paste them inside of backClicked and forwardClicked, and then inside of backClicked, above the two lines that you pasted, type main_mc.prevFrame, p-r-e-v, short for previous, Frame, capital F, some parenthesis and a semicolon.

So this will move back one frame, and we can just copy and paste this line of code into forwardClicked, above the rest of the code that's already in there, change prev to next, so it's nextFrame, capital F. Test the movie. You can click the Play button to start the animation. Once the bear comes on the Stage, you can press Forward button to advance in frames or the Back button to step back in frames. Then you can press the Play button to continue the animation.

So you've seen that you can navigate through the Timelines of various objects, including animations, by using several built-in methods, like nextFrame, prevFrame, Play, and Stop, and you can control the visibility and interactivity of objects using the visible property.

Using common timeline navigation methods
Video duration: 5m 34s 7h 8m Beginner Updated May 23, 2012


Using common timeline navigation methods provides you with in-depth training on Developer. Taught by Todd Perkins as part of the ActionScript 3.0 in Flash Professional CS5 Essential Training

please wait ...