Tutorial:Timeline ActionScript (AS2)

From Anipedia

Jump to: navigation, search
ActionScript can help you do some very helpful things with the timeline in Flash. In this tutorial you will learn:
  1. How to manipulate the timeline through ActionScript 2.0
  2. How to create buttons that manipulate the timeline

[edit] Step 1: How to stop the timeline

So, you've created a fantastic Flash movie, but there's a problem: When the movie reaches the end, the Flash movie loops. This can be easily fixed with basic ActionScript.

It's a good idea to make a new layer on your timeline dedicated to ActionScript. It is not necessary, but good organization is a key to good ActionScript. To create a new layer, click on the "Insert Layer" button, on the bottom left of the timeline, or right click on the title of a pre-existing layer and click on "Insert Layer".

In your new ActionScript layer, place a key frame on your animation's last frame. Click on it so that it is selected. Now we can edit the ActionScript associated to this key frame.

Click on the actions window. If it is not open, you can open it by hitting F9 or clicking on "Window" > "Actions". In the large white box, type stop(); Now test your movie, it should stop on the frame now.

[edit] Step 2: Creating buttons to manipulate the timeline

Buttons can be extremely useful in Flash. A replay button can be used to let the user watch the movie again. Please view this tutorial for directions of how to create a button, and additional information.

With your button placed on the key frame we made the timeline stop at, it is now time to add a Frame Label on the first frame of your animation. Click on the first frame of your animation, and insert a key frame in your ActionScript layer. Click on the key frame to select it. In the Properties window, notice the white box with the text <Frame Label> written in it. Click on the box and write a label for it. For this example, type the word "frame" into the box.

Now return to the frame that you placed your button on. Click on the key frame. Now click on your button. It is now time to type in the Actions window again. The window should now read: "Actions - Button".

If it does not, and the input field reads, "Current selection cannot have actions applied to it." you must switch the type of ActionScript from 3.0 to 2.0. To do this, de-select the button, and click on the properties window. Now click on "Settings..." next to "Publish:" On the Flash tab, change "ActionScript version:" from "ActionScript 3.0" to "ActionScript 2.0" and click OK.

Return to the Actions window, making sure that the button is selected and the title of the Actions window reads: "Actions - Button".

Type in this code:

  on(release){
  
     gotoAndPlay("frame");
  
  }

The button should now work, and make the timeline return to the frame which was labeled and play from that point.

Here is an FLA that can be used as an example of what this should all look like: Timeline_ActionScript_Example.fla