Search Flex Samples

flex 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:

  • The addition operator (+) adds two values together, resulting in a single number:
    var sum:Number = 23 + 32;

  • The multiplication operator (*) multiplies one value by another, resulting in a single number:
    var energy:Number = mass * speedOfLight * speedOfLight;

  • The equality operator (==) compares two values to see if they are equal, resulting in a single true-or-false (Boolean) value:
    if (dayOfWeek == "Wednesday")

    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:

  • Single-line comment: A single-line comment is designated by placing two slashes anywhere on a line. Everything after the slashes up to the end of that line is ignored by the computer:
    // This is a comment; it's ignored by the computer.
    var age:Number = 10; // Set the age to 10 by default.

  • Multiline comments: A multiline comment includes a starting comment marker (/*), 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.

Flow control

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.

  • Functions: Functions are like shortcuts--they provide a way to group a series of actions under a single name, and can be used to perform calculations. Functions are particularly important for handling events, but are also used as a general tool for grouping a series of instructions. For more on functions, see Functions.
  • Loops: Loop structures let you designate a set of instructions that the computer will perform a set number of times or until some condition changes. Often loops are used to manipulate several related items, using a variable whose value changes each time the computer works through the loop. For more on loops, see Looping.
  • Conditional statements: Conditional statements provide a way to designate certain instructions that are carried out only under certain circumstances or to provide alternative sets of instructions for different conditions. The most common type of conditional statement is the 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
    // do some non-administrator things

    For more on conditional statements, see Conditionals.



Related Flex Samples

Learn Flex: Flex Samples | Flex Video Tutorials Flex Examples