Macromedia Flash MX 2004 ActionScript Programming Tutorial

Macromedia Flash MX 2004 ActionScript 2.0 Tutorial and Free Online Training Course

In this section, you will learn about:

  • If Statements
  • Switch() and Case Statements

Conditional Statements

If Statements

Conditional statements test whether something is true or false and, if true, perform a set of actions:

if (this is true) {

do this;


The code above forms a basic if statement. The condition being tested appears in the parentheses next to “if”. The actions to be performed if the condition is true appear within the curly braces. For example, the following code tests whether or not the value of the variable age is greater than or equal to 18. If it is, the current timeline moves to Frame 2.

if (age >= 18) {



Often, you’ll need to test more than one condition at a time. For example, in the code above, what if the value of the variable is less than 18? What should happen then? To test multiple conditions, use an extended if…else if…else statement:

if (condition 1 is true) {

do this;

} else if (condition 2 is true) {

do this;

} else if (condition 3 is true) {

do this;

} else {

do this;


The final “else” tells Flash what to do if none of the preceding conditions is true. Without it, if none of the conditions is true, Flash will do nothing at all.

You can use the Actions toolbox in the Actions panel to help you construct conditional statements. The elements of conditionals are located in the Conditions/Loops subfolder of the Statements folder:

As a simple example, we’ll build a movie that displays a message customized to the user’s age:

  1. We’ve already created a movie with two keyframes. The first keyframe contains an input text field, a static text label that asks for the user’s age, and a button symbol. The variable assigned to the input text field is age, which we entered into the var field of the Property Inspector. The button advances the timeline to the second frame, where the customized message appears:

  1. In the first frame of the Actions layer, we assigned string values to three variables: age1, age2, and age3. These values are the specific messages that will be displayed, depending on the user’s age:

We also added a stop action to prevent the timeline from advancing until the user clicks the button.

  1. The second frame contains a dynamic text field, which we’ve called “age_txt”. It also contains a button that moves the timeline back to the first frame, so the movie can be tested repeatedly to see the different messages:

  1. Now we need to add the conditional statement that will test the age the user enters into the input text field in Frame 1 and display the appropriate response. In the second frame of the Actions layer, open the Actions panel, navigate to the if action in the Actions toolbox, and double-click it:

Flash inserts the parentheses and curly braces and displays a code hint that tells you to insert a condition into the parentheses.

  1. Between the parentheses, type:

age < 18

  1. Move the cursor between the curly braces and insert the code that will assign the contents of the age1 variable to the dynamic text field:

age_txt.text = age1;

  1. To insert the second condition, double-click on the else if action in the toolbox:

  1. For the second condition, we want to test whether or not the value is between 18 and 34. To do this, type the following between the parentheses:

age >= 18 && age < 35

The && (AND) operator tells Flash to evaluate both conditions and, if both conditions are true, to perform the action.

  1. For the action, assign the contents of the age2 variable to the dynamic text field by typing:

age_txt.text = age2;

  1. We’ll use the third condition to perform a default action—this is the action that will be performed if neither of the preceding conditions is true. In the Actions toolbox, double-click on the else action:

  1. Type:

age_txt.text = age3;

  1. Add a stop action to the next line, outside the if statement’s curly braces, to prevent the timeline from advancing.

When you’ve finished, the code should look like this:

  1. Test the movie, entering different ages to see the customized messages.

One Comment

  1. zoe

Leave a Reply

Your email address will not be published. Required fields are marked *