Email the Instructor

Upload a File

Flash 1 - Beginning

Class ends Sunday June 29, at 5pm

×

Drag your file here to upload it.

ActionScripting

Any degree of interactivity in your Flash movie requires ActionScripting—programmatic commands that tell the movie what to do at certain points. ActionScripts are placed on keyframes (in any timeline) via the Actions panel, found in WINDOW > Actions.

NOTE: "Action", "ActionScript" and "Script" are often used interchangably. Technically there is a difference:
An Action is one specific statement within an ActionScript (which can consist of many different individual Actions). ActionScript is the complete set of programming statements in a keyframe of a timeline; it is also the name of the language used to program in Flash.
A Script is a generic term for a sequence of programming statements in any language (like PHP, JavaScript, C++ or ActionScript).

The Actions panel:

Adobe has dramatically changed the organization of Action categories in the last several versions of Flash (and may do so in future versions) so we will not use the left pane of the Actions panel. In fact, you can collapse the left half of the panel to make more room in the right half, by clicking the thin button in the center of the panel. The right pane is a text editor.

NOTE: In earlier versions of ActionScript (2.0 and earlier) it was possible to attach scripts to button and movie clip instances. This is no longer possible in ActionScript 3.0. You can only place ActionScripts on keyframes.

TIP: you can increase the font size—and even change the font—used in the Actions panel by selecting Preferences... from the Actions Panel drop-down menu at its top right corner.

Adding Actions to your movie

  1. Open your Chopper movie from the previous exercise.
  2. Add a layer named "Actions"
  3. Select the keyframe in frame 1 of the Actions layer.
  4. type the following script in the Actions panel:

stop();

The stop action does just what it sounds like it does: it prevents the timeline from playing past that point (it's also the easiest ActionScript you'll ever write).

Next, you'll add a button that plays the movie:

  1. Add a new layer to your movie and name it "Button."
  2. Open the Common Buttons library:
    CS6 users: WINDOW > Common Libraries > Buttons
    CC Users: Flash CC removed the Common Libraries; you can download it here. After unzipping, choose FILE > Import > Open External Library... and target the Buttons.fla file (ignore the warning About ActionScript 2.0)
  3. Open the Classic Buttons folder (by double-clicking it), and open the Arcade Buttons set. Drag the Arcade Button-Red to your stage (after confirming the Button layer is the active one!—just like in Photoshop it's important to pay attention to which is the active layer!).
  4. Buttons can only work if they have instance names. Select the instance of the Arcade Button-red onstage, and name it "red_btn" on the Properties panel (you get to make up button names, but adding "_btn" ensures you don't have naming conflicts with other ActionScript keywords).
  5. In frame 1 of the Actions layer, add to the stop(); action already there:

    (you also get to make up function names. It's standard practice to make them descriptive, and they MUST be a single word)
    This script does two things: it defines a function called "playChopper". It then adds an event listener to the red button that will call that function when the button broadcasts the 'click' mouse event. Event listeners are exactly what they sound like: it's a programmatic construct that listens for a specific event to happen.
  6. Select the last frame of the Actions layer (note that it is not a keyframe, so the ActionScript you see in the Actions panel is really associated with frame 1, NOT the selected frame!).
  7. Press F6 to insert a keyframe.
  8. Add a stop(); action to this keyframe, preventing the movie from looping back around to the beginning.
  9. Test the movie.
    The stop action halts it before the chopper takes off —note that the stop action only stops the timeline it's in; the rotors ignore this action!—and the red button plays the stopped timeline.
    The final stop action prevents the main timeline from looping.
    However the red button is still listening for click events, and if detected, will still play the (stopped) timeline, looping it back to frame 1 (where the initial stop action again halts playback).

Case sensitivity

ActionScript is case-sensitive: gotoAndPlay(1); will work; GoToAndPlay(1); will not work. ActionScript largely ignores whitespace (it does not matter if you leave blank lines in a script, nor if you have spaces around mathematic operators such as + or =, but you cannot break words apart:
goto AndPlay(1);
will not work).

ActionScript syntax

ActionScripts (a.k.a. scripts) are made up of individual actions (a.k.a. statements). An ActionScript can be a single statement, or many statements. Statements generally have one of four structures:

variable = value;    assigns a value to something

functionCall();    performs a previously defined function

functionDefinition(parameter){
    statement block;
  }
                                  defines a function to be performed later

object.addEventListener(event, function);  tells an object to listen for an event, and which function to perform when the event happens

Controlling timelines

There are several actions that control movie clips (and since Scene 1 is really a type of movie clip, these work in the main timeline as well as inside movie clip timelines):

stop();
play();
nextFrame();
prevFrame();
gotoAndStop(#);
gotoAndPlay(#);

NOTE: the “#” would be replaced by an actual frame number or frame label in the goto... actions above.