Showcase and discover digital art at yex

Follow Design Stacks

Subscribe to our free newsletter to get all our latest tutorials and articles delivered directly to your inbox!

About Syntax and Statements

About Syntax and Statements

Learning ActionScript syntax and statements is like learning how to put together words to make sentences, which you can then put together into paragraphs. ActionScript can be just as simple. For example, in English, a period ends a sentence; in ActionScript, a semicolon ends a statement. In the ActionScript language, you can type a stop() action to stop the playhead of a movie clip instance or a SWF file from looping. Or you can write thousands of lines of code to power an interactive banking application. As you can see, ActionScript can do very simple or very complex things.

In the Understanding Data section, you learned how the ActionScript language uses data, and how you can format syntax. This section demonstrates how you can form statements in ActionScript using syntax. It contains many short code snippets and some examples to demonstrate fundamental language concepts. Upcoming sections contain longer and increasingly involved code examples that combine and facilitate the fundamentals you learn in this chapter.

Looking at Language Elements

The ActionScript language is made up of the built-in classes that make up the ActionScript language. You need to use correct ActionScript syntax to form statements so the code compiles and runs correctly in Flash. In this case, syntax refers to the grammar and spelling of a language that you program with. The compiler cannot understand incorrect syntax, so you see errors or warnings displayed in the Output panel when you try to test the document in the test environment. Therefore, syntax is a collection of rules and guidelines that help you form ActionScript correctly.

A statement is an instruction you give the FLA file to do something, such as to perform a particular action. For example, you can use a conditional statement to determine whether something is true or exists. Then you might execute actions that you specify, such as functions or expressions, based on whether the condition is true or not. The if statement is a conditional statement and evaluates a condition to determine the next action that should occur in your code.

// if statement
if (condition) {
// statements;
}

Expressions, different from statements, are any legal combination of ActionScript symbols that represent a value. Expressions have values, while values and properties have types. An expression can consist of operators and operands, values, functions, and procedures. The expression follows ActionScript rules of precedence and of association. Typically, Flash Player interprets the expression and then returns a value that you can use in your application. For example, the following code is an expression:

x + 2

In the previous expression, x and 2 are operands and + is an operator.

The way you format your ActionScript also determines how maintainable your code is. For example, it’s difficult to read the logic of a FLA file that doesn’t contain indents or comments, or contains inconsistent formatting and naming conventions. When you indent blocks of ActionScript (such as loops and if statements), the code is easier to read and debug if you encounter problems.

In ActionScript, you use a dot (.) operator (dot syntax) to access properties or methods that belong to an object or instance on the Stage. You also use the dot operator to identify the target path to an instance (such as a movie clip), variable, function, or object. A dot syntax expression begins with the name of the object or movie clip, followed by a dot, and it ends with the element you want to specify. The following sections demonstrate how to write dot syntax expressions.

To control a movie clip, loaded movie, or button, you must specify a target path. Target paths are hierarchical addresses of movie clip instance names, variables, and objects in a SWF file. To specify a target path for a movie clip or button, you must assign an instance name to the movie clip or button. You name a movie clip instance by selecting the instance and typing the instance name in the Property inspector. Or you can specify the instance name with code if you create the instance using ActionScript. You can use the target path to assign an action to a movie clip or to get or set the value of a variable or property.

Note: When you write ActionScript for Flash Player 7 and later, your code is case-sensitive. This means that variables with slightly different capitalization are considered different from each other.

Punctuators are the characters that help form your ActionScript code. There are several language punctuators in Flash. The most common type of punctuators are semicolons (;), colons (:), parentheses [()] and braces ({}). Each of these punctuators has a special meaning in the ActionScript language and helps define data types, terminate statements or structure ActionScript. For more information, see About Language Punctuators (Learning ActionScript 2.0 > Syntax and Language Fundamentals > About Language Punctuators).

Constants are properties with a fixed value that cannot be altered, so they are values that don’t change throughout an application. Flash includes several predefined constants, which can help simplify application development. An example of constants can be found in the Key class, which includes many properties, such as Key.ENTER or Key.PGDN. If you rely on constants, you never have to remember that the key code values for the Enter and Page Down keys are 13 and 34. Using constant values not only makes development and debugging easier, but it also makes your code easier to read by your fellow developers.

Keywords in ActionScript are used to perform specific kinds of actions. They are also reserved words because of this, so you can’t use them as identifiers (such as variable, function, or label names). Examples of some reserved keywords are if, else, this, function, and return.

An array is an object whose properties are identified by numbers representing their positions in the structure. Essentially, an array is a list of items. Unlike some languages, ActionScript does not require that each element in an array is of the same data type. You can mix numbers, dates, strings, and objects and even add a nested array at each array index.

Operators are characters that specify how to combine, compare, or change values in an expression. An expression is any statement that Flash can evaluate and that returns a value. You can create an expression by combining operators and values or by calling a function.

For example, a mathematical expression uses numerical operators to manipulate the values you use. Examples of operator characters are +, <, *, and =. An expression consists of operators and operands, and they are any legal combination of ActionScript symbols that represent a value. An operand is the part of your code that the operator performs actions on. For example, in the expression x + 2, x and 2 are operands and + is an operator.

Comments