User Guide

if 325
if
Availability
Flash Player 4.
Usage
if(condition) {
statement(s);
}
Parameters
condition
An expression that evaluates to true or false.
statement(s) The instructions to execute if or when the condition evaluates to true.
Returns
Nothing.
Description
Statement; evaluates a condition to determine the next action in a SWF file. If the condition is
true, Flash runs the statements that follow the condition inside curly braces ({}). If the condition
is
false, Flash skips the statements inside the curly braces and runs the statements following the
curly braces. Use the
if statement along with the else and else if statements to create
branching logic in your scripts.
The curly braces (
{}) used to enclose the block of statements to be executed by the if statement
are not necessary if only one statement will execute.
Example
In the following example, the condition inside the parentheses evaluates the variable name to see if
it has the literal value
“Erica”. If it does, the play() function inside the curly braces runs.
if(name == "Erica"){
play();
}
The following example uses an if statement to evaluate how long it takes a user to click the
submit_btn instance in a SWF file. If a user clicks the button more than 10 seconds after the
SWF file plays, the condition evaluates to
true and the message inside the curly braces ({})
appears in a text field thats created at runtime (using
createTextField()). If the user clicks the
button less than 10 seconds after the SWF file plays, the condition evaluates to
false and a
different message appears.
this.createTextField("message_txt", this.getNextHighestDepth, 0, 0, 100, 22);
message_txt.autoSize = true;
var startTime:Number = getTimer();
this.submit_btn.onRelease = function() {
var difference:Number = (getTimer()-startTime)/1000;
if (difference>10) {
this._parent.message_txt.text = "Not very speedy, you took "+difference+"
seconds.";
CHAPTER 2
ActionScript Language Reference