FLASH™ ART - Critical Interactive Multimedia

Conditional Logic

Testing Conditions

simple condition:

if ( < this condition is met > ) {
    
< then execute these lines of code >
}
The condition between the parentheses can be any valid expression that can be evaluated as TRUE or FALSE. If the condition is met (TRUE) then the code between the curly braces will be executed, otherwise, it will not be executed, nothing happens.
You can include as many lines of code to be executed as you wish.
Example Usage:  
if ( _x > 550 ) {
     _x = 0
}
If the value of the _x property is greater than 550, then set the value of the _x property to zero

 

Using the if ... else statement chooses between two blocks of code:

if (< condition > ) {
    <
statement 1 >
} else {
    <
statement 2 >
}

If the condition between the parentheses is TRUE, then the first block of code, statement 1 will be executed. If the condition is FALSE, then the second of block code, statement 2 will be executed.
Example Usage:  
if ( _x > 550 ) {
     _x = 0
} else {
     _x = _x+10
}
If the value of the _x property is greater than 550, then set the value of the _x property to zero (move the movie clip to the left of the stage).
If _x is NOT greater than 550, then set the value of the _x property to the current value of the _x property plus ten (move the movie clip ten pixels to the right).

 

Using the if ... else if ... else statement to choose between multiple blocks of code:

if ( < condition 1 > ) {
    <
statement 1 >
} else if (
< condition 2 > ) {
    <
statement 2 >
} else
{
    <
statement 3 >
}

If condition 1 is TRUE, then statement 1 will be executed.
If condition 1 is FALSE, and condition 2 is TRUE, then statement 2 will be executed.
If both of the conditions are FALSE, then statement 3 will be executed.

You can use as many conditions as you wish in this construction.
Example Usage:  
if ( a > b ) {
     _x = 0
} else if ( a < b ) {
     _x = 550
} else {
     _x = _x+1
}
If the value of the the variable a is greater than the value of b, then set the value of the _x property to zero.
If a is less than b, then set the value of the _x property to to 550.
If a is neither greater than, nor less than b (that is, a is equal to b), then move the movie clip one pixel to the right.

 

Weird equalities

= vs. ==

SYMBOL OPERATION EXAMPLES ENGLISH
= assignment myVar = 17 Assign the value 17 to the variable "myVar"
_x = 17 "set the value of the property _x to 17"
== comparison if(_x == 100){
   myVar = 10
}
if the _x property is equal to 100, set the variable "myVar" to 10

more comparisons

SYMBOL OPERATION EXAMPLES ENGLISH
!= NOT EQUAL
tests for inequality
if(myVar != 100){
   myVar = 100
}
if "myVar" is NOT equal to 100, set the variable "myVar" to 100
>= greater than or equal to if(myClip._x >= 100){
   myVar = 10
}
if the _x property of "myClip" is greater than or equal to 100, set the variable "myVar" to 10
<= less than or equal to if(myClip._x <= 100){
   myVar = 10
}
if the _x property of "myClip" is less than or equal to 100, set the variable "myVar" to 10

 

Checking for more than one condition at once:&& (AND);|| (OR)

if ( (_x < 550) && (_x > 0)) {
     _x = _x+1
}
If the _x property is less than 550 AND the _x property is greater than zero, then add one to the _x property.
note: extra parentheses are used for grouping; they are optional, but they help reading the code.
Example Usage:  
if ( (_x > 550) || (_x < 0)) {
     onStage = false
}
If the _x property is greater than 550 OR the _x property is less than zero, set the variable onStage to false.

Practical Examples, for changing the direction of horizontal movement, or,
Setting Limits to Behaviors:

onClipEvent(load){
    var Xdirection = 1;
}


onClipEvent(enterFrame){

    _x = _x + (10*Xdirection);

    if(_x > 550){
         Xdirection = -1;
    }else if (_x < 0){
         Xdirection = 1;
    }

}
How can I make a moving clip change directions?

This script uses the "load" onClipEvent to initialize a variable Xdirection, and to set it to 1.

Each time the enterFrame event is triggered, the script moves the movie clip either 10 pixels or -10 pixels, depending on the value of Xdirection.
When the _x property exceeds 550, Xdirection is set to -1. When the _x property falls below zero, Xdirection is set to 1.

(note: if you omit the "var" which is supposed to indicate the declaration/initialization of a variable, Flash will forgive you in this case. But, it's good form, and can be important in different contexts.
Likewise: each line of code should be terminated with a semi-colon, but ActionScript is forgiving, and understands that a new line indicates a new statement)

A Moving Target:

onClipEvent(load){
    var Xtarget = random(550);
}

onClipEvent(enterFrame){

    if(_x < Xtarget - 5) {
        _x = _x + 5;
    }else if (_x >Xtarget + 5){
        _x = _x - 5;
    } else {
        Xtarget = random(550);
    }

}

This routine chooses a random target value, Xtarget. It changes the target value for _x, when the movie clip comes within 5 pixels of the target value.