Intermediate Digital Media
Instructor: B. Lattanzi
rev. 11.03

ActionScript for Flash

ActionScript is the scripting language associated with Flash.

"Scripting" and "programming" are 2 terms given to the use of coded instructions to control computer events. Scripting languages are designed to resemble "natural languages" to make them easier to use. The computer is able to take the programming code and translate it, through a series of steps that vary depending on the programming language, into the binary 0's and 1's that the machine needs to accomplish the instructions given to it. ActionScript is one of many programming languages developed in the past several decades.

Flash offers a way of controlling audio and visual events with a visual interface (timeline, stage, library elements). If the final experience is to be interactive, however, it is necessary to expand the means by which Flash controls the computer's resources and the means by which Flash responds to the viewer's interaction. This is done with ActionScript.

Use ActionScript to do such things as:

  1. Navigate the Flash movie interactively and non-linearly using mouseclicks, keypress or other events.
  2. Start and stop sounds independently from the animation with triggering events.
  3. Start and stop, independently, individual videos and animations.
  4. Animate graphic elements by using code to change location, size, and monitor or change many other properties.

For our purposes, we will need to use ActionScript to:

  1. Accomplish navigation: we can use ActionScript for frame events, as well as use Button Symbols in combination with ActionScript to handle mouseclick events.
  2. Control Sounds: we can use Button Symbols in an unusual way that handles keypress events rather than mouseclicks.
  3. Control the occurence of Videos and graphic Animations: we can use MovieClip symbols in combination with ActionScript.

About the ActionScript window:

Menu: Window> Actions

There are 2 modes for writing ActionScripts, "Normal" and "Expert".

We will be using "Expert". It is arguably easier in the long run to start using "Expert" mode right away. For one thing, it "forces" a clearer understanding of what you are doing with the simple scripts. This means that you have a basis of understanding for building more complex scripts later on.

NAVIGATION script examples:

A Frame script is a script that is placed in a blank keyframe in the Flash timeline.

Examples of frame scripts:

gotoAndPlay(56);

gotoAndPlay("red animation");

gotoAndPlay("scene 2", 45);

gotoAndPlay("daylight scene", "bird");

play();

stop();

A Button script is a script that is "attached" to a button on the stage. (Click on the button instance on the stage to select it. Then, type into the ActionScript window.)

Examples of Navigation scripts attached to a button:

on (press){
  
gotoAndPlay("Scene 3", "start frame");
}

on (release) {
   stop();
}

You can also use keypresses to navigate your Flash movie by attaching a script to a button that can be located off-stage because it doesn't need to be clicked on! The button's only function is to hold the ActionScript that is monitoring what key gets hit on the keyboard!

Note that upper-case, lower-case matters in ActionScript. "B" is different from "b", for example.

on (keyPress "c") {
   gotoAndPlay(14);
}

Sounds can be turned on and off independently of the timeline.

Sounds that are only in the Flash Library (and not in the timeline) can be "called" and controlled by ActionScript. Example of scripts attached to one button, controlling SOUNDS on/off. Other properties to control are number of loops of one sound, volume, fade. (Check the ActionScript dictionary in the Flash Help Menu files for more.)

on (keyPress "a") {

// First, turn off the dog sound if it is already playing:

  dog.stop("dogbarkLink");

// Now play the dogbark sound

   dog = new Sound();
   dog.attachSound("dogbarkLink");
   dog.setVolume(100);
   dog.start(0, 3);
}

on (keyPress "z") {
  dog.stop("dogbarkLink");
}

on (keyPress "s") {
  
// First, turn off the morning sound if it is already playing:

   morning.stop("morningLink");

// Now play the morning sound

   morning = new Sound();
   morning.attachSound("morningLink");
   morning.setVolume(100);
   morning.start(0, 8);


   // the last line above says where in the sound to start, i.e., number of offset seconds (0) and the number of times to loop the sound (8)

}

on (keyPress "x"){
   morning.stop("morningLink");
}

Note that the above scripts have some lines that are preceded by a double-slash, //.

These are "commented out" lines of code. This means that the computer ignores them. Any lines that begin with //, are just for your own information and commentary about whatever is helpful for you to note. (All programming languages provide for a way of inserting notes like this. In contrast to ActionScript's //, HTML code for webpages uses combination of brackets, dashes and exclamation point to set apart comments from the rest of the code.)

Nonlinear NAVIGATION EXERCISE:

example:

gotoAndPlay("red section", "begin");

When you get to last frame of the 3rd scene, make a script that will stop the movie:

stop();

In fact, even without your frame scripts, the movie will play the scenes in the order that they appear in the script window. However, ActionScript lets you change the order.

INSERT> New Symbol...

Make a button symbol.

In the library drag the symbol into its own layer, on the very last frame in your final scene.

Attach an ActionScript to it that handles a mouseclick on the button. The script should make your movie go back to the beginning of the first scene:

on (release){
  gotoAndPlay("first scene", "start frame");
}

SCRIPTING BUTTONS TO HANDLE KEYPRESS EVENTS.

Go back to the ActionScript for your button. Add some events to your button that tell the movie to go to the middle of the 2nd scene, using one key. Or, if the viewer uses a different key, the movie goes to the start of the 3rd scene.

on (keyPress "x") {
   gotoAndPlay("scene b", 5);
}

on (keyPress "z") {
   gotoAndPlay("final section", 1);
}

Note that you can attach ActionScripts for keypresses like above script to a button that is "invisible" located off-stage (place it to the left of the stage or above the stage... not to the right or below the stage).

Use an invisible button to handle KEYPRESS control of SOUNDS.

Import 2 sounds into the Library. You will not place the sounds on the timeline. They are, rather, called by ActionScripts attached to an off-stage button.

Refer to the sound script examples above. Note that you must give the sounds a "linkage" identifier name. This is a special name that you type into a dialogue window for the sound that can be accessed by a tab in the upper right corner of the Library window. In the linkage dialoge window, click on "Export for ActionScript", then type a linkage identifying name that all one word (no spaces or punctuation).

Video and Animations with ActionScript will be described in conjunction with the 3rd Symbol type in Flash... the MovieClip Symbol.

Insert> New Symbol...

Make one MovieClip Symbol. Name it "placeholder".

Note that MovieClips allow you to load and unload both internal and external Flash movies and make them play inside of your main Flash movie

Use empty MovieClips as placeholders...

Use empty movieclips to load and unload external .swf movies

Make a new layer in the 3rd scene of the movie. Drag the empty movieclip to that layer on the stage. If you cannot find the small dot that marks the location of your empty movieclip then look in the area off-stage, to the upper-left. It sometimes lands there. Drag it to the location where you want it on the stage.

Click on the MovieClip on the stage and check the Property window where you should give the MovieClip instance a name, for example, "placeholder1". (Remember that an "instance" is just a representation on the stage of a symbol in the library. You can have more than one "instance" of any symbol in the library.)

Add new keypress instructions to the ActionScript attached to the button in the same scene, as in the following example...

EXAMPLE SCRIPTS...

for loading an external Flash movie (an .swf file) , jumping to different frames in that movie, and unloading that movie in order to load another external Flash movie.

on (keyPress "<Up>") {
  _root.placeholder1.loadMovie("treecuttingmovie");
}

on (keyPress "<Right>"){
  _root.placeholder1.gotoAndPlay(40);
}

on (keyPress "<Left>") {
  _root.placeholder1.gotoAndPlay(100);
}

on (keyPress "<Down>") {
  _root.placeholder1.unloadMovie();
  _root.placeholder1.loadMovie("cloudsmovie");
}

Example of loading and unloading .swf files in the Flash Library

Use the ActionScript function called "createEmptyMovieClip()" along the "attachMovie()" function for flexibly loading .swf files that you have imported into the Flash library.

on (keyPress "g") {

// create 2 empty movie symbols, give them each an arbitrary name in quotes and use a number to indicate what "layer" (similar to Flash timeline layers) where each symbol will be located (the higher the number, the more "on top" the layer).

_root.createEmptyMovieClip("formyredmovie", 0)

_root.createEmptyMovieClip("formybluemovie", 2)

// Now refer to each empty movie symbol by name, and use the "attachMovie()" function to use these empty movies like containers to hold movie symbols in the Flash library

_root.formyredmovie.attachMovie("redflash", redID, 3)

_root.formmybluemovie.attachMovie("blueflash", blueID, 1)

// With the ID names (here arbitrarily called "redID" and "blueID") you can control properties of specific instances of your clips, such as their x, y locations. For example:

_root.formyredmovie.redID._x = 80;

_root.formyredmovie.redID._y = 180;

}

on (keyPress "h") {

// To make any movie symbol (or .swf file) in the library disappear, you can place a new empty movie clip in the same layer (i.e., using the same layer number)

_root.createEmptyMovieClip("thisemptyclip", 0)

// An easier way to do this is with the "removeMovieClip();" function

_root.formyredmovie.removeMovieClip();

_root.formybluemovie.removeMovieClip();

}