ActionScript

Beginning with Flash CS3, Flash uses ActionScript 3.0 which is like Java whereas Flash 8 used ActionScript 2.0 which is like JavaScript. When one creates a new file in CS3 and later, one chooses between ActionScript 2.0 and 3.0. This writeup addresses ActionScript 3.0

ActionScript 3.0 Elements

variables - hold data

var score: Number = 0;

This is doing two things. The use of the var keyword means that we are declaring a variable (score) to be of a certain type (Number). Secondly, this is assigning the value 0 to the variable. If one has already declared a variable, one does the assignment without the var keyword, e.g.

text:String = "Hello";

The use of the double quotes denotes a string value

Instances and instance names

To name an instance, select the instance and at the left side of the Property Inspector, type the name. Names should begin with a lower case and one should avoid spaces and special characters. In versions of Flash, If one ends instance names with _mc (movie clip), _btn (buttons), and _txt (text fields), Flash will give you access to code hints in the Actions panel once one types the instance name followed by a period (intellisense). Unfortunately this has not been present in recent versions although the naming convention is still a useful one so as to keep organized.

Properties

Properties are variables that are attached to an instance of a symbol. Alternatively, you can think of properties as attributes that you can modify in the Property Inspector. In ActionScript 3.0, properties will be accessed using dot syntax, i.e.

instancename.property

Functions and Methods

Methods will be followed by parentheses - sometimes there will be something between the parentheses (these are called arguments). Functions will be defined using the function keyword, as in:

function myFunction():void {Block of code}

The void keyword indicates that the function does not return a value

Using ActionScript to Control the Main Timeline

function playMovie(event:MouseEvent):void {play();}
play_btn.addEventListener(MouseEvent.CLICK, playMovie);

Here

To test the movie, select Control>Test Movie

The movie will play once and stop in frame 1 due to the presence of the stop() action in frame 1. To avoid this, click in the final frame of the movie, in the Actions layer, and type gotoAndPlay(2) in the Actions Panel. This will take the movie to frame 2, bypassing frame 1 and the stop action.

Note: If the difference between frame 1 and 2 is noticeable, one can remove any tweens that start at frame 1, then click on frame 1 and press F6 to copy over the content to frame 2, and then begin any tweens that previously started at frame 1, at frame 2 instead.

Using ActionScript to Control Movie Clips

One simply gives the instance of the movie clip a name through the Property Inspector and then in the ActionScript, one prefaces the methods such as stop(), play(), and gotoAndPlay(2) with the instance name of the movie clip followed by a period. Thus if the instance of the movie clip was named ball_mc, one would type ball_mc.play();

Revised: November 21, 2011. Comments to William Pegram, bill@billpegram.com