![]() ![]() ![]() | |
![]() | |
![]() | |
![]() |
Flash Player 4.
if(condition) {
statement(s)
; }
condition
An expression that evaluates to true
or false
.
statement(s)
The instructions to execute if or when the condition evaluates to true
.
Nothing.
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
action to create branching logic in your scripts.
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()
action inside the curly braces runs.
if(name == "Erica"){ play(); }
The following example uses an if
action to evaluate when a draggable object in the SWF file is released by the user. If the object was released less than 300 milliseconds after dragging it, the condition evaluates to true
and the statements inside the curly braces run. Those statements set variables to store the new location of the object, how hard it was thrown, and the speed at which it was thrown. The timePressed
variable is also reset. If the object was released more than 300 milliseconds after it was dragged, the condition evaluates to false
and none of the statements run.
if (getTimer()<timePressed+300) { // if the condition is true, // the object was thrown. // what is the new location of this object? xNewLoc = this._x; yNewLoc = this._y; // how hard did they throw it? xTravel = xNewLoc-xLoc; yTravel = yNewLoc-yLoc; // setting the speed of the object depending on // how far they travelled with it xInc = xTravel/2; yInc = yTravel/2; timePressed = 0; }
![]() | |
![]() | |
![]() | |
![]() ![]() ![]() |