G-L > if |  |

if

if, conditional

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

Action; evaluates a condition to determine the next action in a movie. If the condition is `true`, Flash runs the statements that follow the condition inside curly brackets (`{}`). If the condition is `false`, Flash skips the statements inside the curly brackets and runs the statements following the curly brackets. Use the `if` action to create branching logic in your scripts.

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` action inside the curly brackets runs.

    if(name == "Erica"){
    	play();
    }

Example

The following example uses an `if` action to evaluate when a draggable object in the movie 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 brackets 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;
    	}

See also

`else`