Statements are typically made up of keywords and expressions. We've already seen the var statement, which declares, and optionally, initializes a variable. Here's an example of the var statement's general syntax:
var numFrames;
The keyword (var in this case) begins the statement. Each keyword is followed by the syntax (parentheses, parameters, operands, etc.) required to complete the statement, which, in our example, is simply the name of a variable, numFrames. Finally, a semicolon marks the end of the statement.
|
Some statements can take multiple forms. For example, we can use var with or without an optional initial assignment (in this case, 10 is a simple expression whose value is assigned to x):
var x; // Simple declaration var x = 10; // Declaration with assignment
We'll discuss the specific syntax of each statement throughout the rest of this chapter.
Some statements actually include other statements, or substatements, as part of their syntax. For example, the if statement has this syntax:
if (expression) substatement;
The substatement, which is executed only if expression evaluates to true, can be a single statement, such as a variable declaration statement:
if (x = = 5) var numFrames = 2;
or it can be a series of statements grouped together as a statement block:
if (x = = 5) { var numFrames; numFrames = 10; play(); }
As you can see, a statement block is any number of statements on one or more lines, surrounded by curly braces. Here we show three statements, separated by semicolons, all on one line:
{ statement1; statement2; statement3... }
By using a statement block as the substatement of our if statement, we can specify multiple statements to be executed conditionally (only when the if statement is true). This can be very handy.
We can use a statement block anywhere ActionScript expects a single statement. In fact, statement blocks are sometimes required. For example, the function statement must always include a statement block, even if the function being declared has only one statement in its body:
function aheadTwoFrames ( ) { gotoAndStop(_currentframe + 2); }
For the sake of readability, statement blocks are typically formatted as shown here:
parent_syntax { substatement1; substatement2; substatement3; }
where parent_syntax represents the statement for which we are defining a statement block.
Note that the opening curly brace appears at the end of the first line after parent_syntax. The substatements of the statement block appear indented two spaces, each on its own line. The closing curly brace, also on its own line, is flush with the main statement. Substatements within statement blocks should end with a semicolon, but there are no semicolons on the lines with curly braces.
The indenting format is simply a convention, not a requirement of legal syntax. This book adheres to the style used by the Flash ActionScript editor (the Actions panel). The following format is also valid and common:
parent_syntax { substatement1; substatement2; substatement3; }