• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

Part IV: Using Basic Actionscript > Actionscript Syntax

Chapter 15. Actionscript Syntax

by Darrel Plant

In this chapter

Using Numeric Variables

Using String Variables

Using Boolean Variables

Comparing Expressions

Using Functions

Using the if Statement

Using Loops

Using Navigation Commands

Troubleshooting ActionScript Syntax

Did You Know?

Like any language, ActionScript has its own structure, or syntax. Although it's not as flexible as the syntax of most human languages, there is usually more than one way to write a line of code to get the same result. The important thing is to follow the rules. Computers aren't very good at figuring out what you want them to do; they just follow your orders to the letter.

Scripts in timeline frames can contain any number of lines of ActionScript code. The lines are executed in order from top to bottom when the playhead reaches the frame at which the script appears. Each line, not including control structures (such as function or if), ends with a semicolon (;). In this example, a variable is assigned a value, and then the movement of the playhead is stopped:

myVariable = 500;
stop ();

Function blocks start with a line with the keyword function, followed by the name of the function, a pair of parentheses containing parameter definitions, and a pair of curly brackets ({ })containing the instructions associated with the function. This example of a function computes the distance between two points and returns that value:

function distance (x1, y1, x2, y2) {
  var dx = x2 - x1;
  var dy = y2 - y1;
  return Math.sqrt (dx * dx + dy * dy);

Function statements aren't automatically executed along with the rest of the statements in a script. Instead, the function's commands are stored in memory until they're needed. They execute when the function is evaluated, or called. The following is an example of a line of code that calls the distance function, setting a variable called howFar to the distance between a point at (100, 100) and (200, 200):

howFar = distance (100, 100, 200, 200);

Scripts attached to movie clip or button symbol instances can contain both functions and event handlers, but not standalone lines of code. Similar to functions, handlers enclose their lines of code in a pair of curly brackets. Also, keywords related to handlers begin with the word on. This example of a button handler calls the previous distance function and sets a variable to the distance between the cursor position and point (100, 100) at the time the mouse button is pressed:

 on (press) {
  var dx = _xmouse - 100;
  var dy = _ymouse - 100;
  return Math.sqrt (dx * dx + dy * dy);

Each line of a script is a statement. A statement can be as simple as a single-line command, such as stop () or play (), or it can be more complex, assigning a value to a variable, loading a movie, or duplicating a movie clip.

A literal value is an explicit description of a value. Whenever a literal value is used in a statement, the exact value appearing in the statement is used every time. Literal values appear inside double quotes.

An expression is part of a statement that represents some sort of calculated value. Because that value can contain references to data that has changed outside the statement, the value of the calculated expression can change each time it is used.


Like most things having to do with computers, ActionScript can be very exacting. In the case of predefined ActionScript keywords, the language is case sensitive, meaning the computer treats capitalization as part of the spelling of a word. Therefore, the word onMovieClip is different from onmovieclip or onmovieClip. The first one works, but the others don't. To maintain compatibility with earlier versions of Flash, however, user-defined names (for variables and functions) are not case sensitive. You should spell and capitalize words the same way all the time. If you're used to JavaScript tricks that rely on case sensitivity with variable names, however, you'll need to rethink your strategies.



Not a subscriber?

Start A Free Trial

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint