AS Reference  :  Notes Index  :  Resources  :  About/Contact  :  Downloads

ActionScript: Flash's Programming Language

ActionScript (hereafter written as actionscript, so I don't have to find the shift key so often), is the programming language used to control elements of a Flash movie or application. Like any language, spoken or programmed, actionscript follows a defined syntax, or set of rules. When you publish a Flash movie, any actionscript (code) it contains is compiled into a set of bytes which get interpreted by the Flash player when the movie is run in a browser (or as a standalone executable). If the rules are followed, the movie will compile (get published) without any errors, and run with an expected behavior. If the rules are not followed, you may get either compile-time errors (which will be displayed in a window that opens up, called the Output panel, during compilation) or run-time errors (which will cause unexpected behavior when the movie is viewed or interacted with).

All code in this section and following ones (except ASTB Samples) is written in ActionScript 2.0, the version of the language used in Flash MX 2004 and Flash 8. Where components are used, they are V2 Components (used in Flash MX 2004 and Flash 8).

How to add actionscript code to a Flash movie

Since Flash 4, the first version of Flash which came with scripting capability, ActionScript has matured from being a scripting language specific only to Flash to being a class-based language that follows ECMA coding specifications (the same ones that Javascript is based on). Because this transition has happened over a relatively short period of time, and because every new version of the Flash player has been designed to be backwards-compatible and support older coding methods, many different styles of coding may be found in Flash movies today.

In this class (and in the pages of this section, ActionScript Basics), we'll attempt to stick to best practices, as they are defined at the time of this writing. That is, the coding style we'll use is one which is suitable for movies whose target audience uses the Flash 8 or Flash 7 player, and one which allows all of the code in the movie to be found and changed easily. The latter may sound like a silly requirement unless you've ever been at the receiving end of a Flash source file (fla) which you have to modify or maintain and which was written with code put inside movieclips and on various buttons and movieclips within the file. With the event-handling mechanisms of Flash 7 (MX 2004) and 8, there is no longer a good reason not to consolidate all of the movie's code into one frame of the main movie, even "button" code and other movieclip event handling code. The one exception we'll make to that general rule in this section is to occasionally put one or more stop actions inside a movieclip to define the end of a sequence within that clip, or to "park" a movieclip with more than one frame in its first frame while it awaits a command to make its playhead move.

So, the general rule for adding code to a Flash movie in this class will be to open a new Flash movie, click in frame 1, and open the Actions Panel (F9, or Window, Actions, or by clicking the little arrow-in-a-circle icon on the right side of the Properties panel). You should check the upper left corner of the Actions panel before you start typing to make sure it says "Actions - Frame", to make sure you're typing actionscript into a frame and not onto a movieclip or button.

ActionScript Syntax and Coding Conventions

The Flash compiler is especially picky about some rules of syntax and not so picky about others. For example, every actionscript command should end with a semi-colon (;) to indicate the end of a statement (since statements can be broken out over several lines, even with carriage return/linefeeds between the lines) but no compiler error will be produced if the semi-colon is left off.

Case sensitivity, code hints, and strict typing

Case-sensitivity, on the other hand, is rigorously demanded by the compiler, both for built-in methods (gotoandplay will not be accepted as a valid movieclip method; it must be typed gotoAndPlay), and for user-defined variable and function names. For this reason, if you are just starting out with actionscript, it's a good idea to get in the habit of using certain predefined suffixes for movieclip instance names (the names by which you address a movieclip in actionscript), textfields, and other built-in Flash classes. For movieclips, the suffix is _mc and for textfields it's _txt. If you type either of these onto a line in the Actions panel, followed by a period (.) you'll see a menu of all available properties and methods for that instance popup. Clicking on an item in that menu inserts it automatically (and with the correct case) into your code.

Another way to get the code hints menu to appear for instances of built-in classes is to specify the datatype of your variables. This won't work for movieclips, since they are either already on stage or created dynamically, but not declared in code, but for all other types of variables, you can specify the class of the variable using strict typing, as described here, which will cause code hints to appear when the name of the variable is typed in, followed by a dot (period).

Code blocks

Code blocks are a series of actionscript statements which must be executed together, one statement after the next, under certain conditions. Code blocks must be placed within curly brackets { and }, as described with examples on the page about code blocks. User-defined functions are special cases of code blocks which have been created, named, and stored for future use in the Flash movie. They're also described on the code blocks page.

Expressions

An expression refers to a particular phrase or group of statements to be evaluated, and is contained within parentheses. An if statement, for example, always contains an expression which must be evaluated to see if it's true. If it is, the set of statements within the code block following is carried out. You can also use parentheses to set the order of precedence in a mathematical calculation, as shown on the operators page.

Variable and Function Names

You can name your variables and functions using whatever naming convention suits you (mixed case, lowercase, characters plus numbers) as long as you don't start the name with a number, and don't include a space or hyphen (-) or period (.) in the name. Underscores (_) are fine and dollar signs ($) are fine (and are often used by programmers as a variable name prefix to indicate the variable's specific use -- private variable, local variable or a function parameter, eg).

ActionScript Code Examples

The rest of the pages in this section will look at the concepts introduced above in more detail. To get an overview of the actionscript needed for various common tasks, or to find some specific code to cut and paste into your movie (and modify as needed), check the ActionScript Reference page.

Intro
Flash: What & How
Example Sites
Create
Draw, Edit Shapes
Gradients
More Drawing Tips
Import
A Sample
Animate
Frames, Keyframes
Motion Tweens
More Motion Tweens
Shape Tweens
Masks
Control
Stop/Replay
Movieclips Intro
Movieclip Reference
Site Structure 1
Slideshow Movieclip
Contact Form
Scroll Resume
Preloader
Site Structure 2
Publish
Display Options
Player Detection
Optimize
AS 2.0 Basics
Intro to Syntax
Playhead Commands
Playhead Cmds 2
Coded Tween
onEnterFrame
Intro to Classes
Declare/Assign
Comments, Trace
Simple Data Types
Arrays & Objects
Code Blocks
Operators
Beyond Buttons
Code Structure
Toggle Controls
Group of Buttons
Drag and Hit
Distort Magnifier
Scroll Text
Bee Game
Dart Shooter
Sound Control
Easing Slider
Easing Slider 2
Components Intro
Timers & Delays
Dynamic Content
Intro
Drawing API
Create Text
Attach Movieclips
Easing Slider 3
Easing Slider 4
Load jpg/swf
Sliding Viewer
Preload swf
XML
Easing Slider 5
Server Comm
LoadVars (w/ PHP)
AS - PHP Lookup
Text File
Database 1:LoadVars
Database 2:Remoting
Read from directory
AS 2.0 Classes
Intro
Math
Key
Date
Color
EventDispatcher
New Samples
Pie Chart
Event-model Emailer
Tween Sequence
Fuse Sequence
SVG in Flash
Bitmap Topo
SWF as Data Holder
Two-level Menu
Yahoo! Flash Maps
Class-based Game
ASTB Samples
Disclaimer
3D Outlines
Bounce Collide
Address Book
Save Drawings
Home  :  Notes Index  :  Resources  :  About/Contact  :  Downloads