Flash CS3 Documentation |
|||
| Programming ActionScript 3.0 > Getting started with ActionScript > Common program elements | |||
In addition to declaring variables, creating object instances, and manipulating objects using their properties and methods, there are a few other building blocks that you use to create an ActionScript program.
Operators are special symbols (or occasionally words) that are used to perform calculations. They are mostly used for math operations, and also used when comparing values to each other. As a general rule, an operator uses one or more values and "works out" to a single result. For example:
+) adds two values together, resulting in a single number:var sum:Number = 23 + 32;
*) multiplies one value by another, resulting in a single number:var energy:Number = mass * speedOfLight * speedOfLight;
==) compares two values to see if they are equal, resulting in a single true-or-false (Boolean) value:
if (dayOfWeek == "Wednesday")
{
takeOutTrash();
}
As shown here, the equality operator and the other "comparison" operators are most commonly used with the if statement to determine if certain instructions should be carried out or not.
For more details and examples of using operators, see Operators.
As you're writing ActionScript, you'll often want to leave notes to yourself, perhaps explaining how certain lines of code work or why you made a particular choice. Code comments are a tool you can use to write text that the computer should ignore in your code. ActionScript includes two kinds of comments:
// This is a comment; it's ignored by the computer. var age:Number = 10; // Set the age to 10 by default.
/*), then the comment content, and an ending comment marker (*/). Everything between the starting and ending markers is ignored by the computer, regardless of how many lines the comment spans:/* This might be a really long description, perhaps describing what a particular function is used for or explaining a section of code. In any case, these lines are all ignored by the computer. */
Another common use of comments is to temporarily "turn off" one or more lines of code--for example, if you're testing out a different way of doing something, or trying to figure out why certain ActionScript code isn't working the way you expect.
Many times in a program, you will want to repeat certain actions, perform only certain actions and not others, perform alternative actions depending on certain conditions, and so on. Flow control is the control over which actions are performed. There are several types of flow control elements available in ActionScript.
if statement. The if statement checks a value or expression in its parentheses. If the value is true, the lines of code in curly braces are carried out; otherwise, they are ignored. For example:
if (age < 20)
{
// show special teenager-targeted content
}
The if statement's companion, the else statement, lets you designate alternative instructions to be performed if the condition is not true:
if (username == "admin")
{
// do some administrator-only things, like showing extra options
}
else
{
// do some non-administrator things
}
For more on conditional statements, see Conditionals.
Flash CS3
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/flash/9.0/main/00000025.html