Flash Conditionals and Loops
syllabus | schedule | assignments | Web Resources | ARTC courses
[Flash Intro]     [Drawing]     [Other Tools]     [Modify/Import]     [Symbols]     [Animation]     [Navigation]     [Publishing]    [Audio]     [ActionScript]     [Textboxes]     [Conditionals/Loops]     [Functions]     [Timing]     [Interaction]    [KeyPressed]    
Conditionals: if statements and switches.

• Conditionals are the decision-making mechanisms within code. They are either true or false.

-Ex: if (this is true){

do this;
do that;

}

-Ex: if (myVariable < 10){

myMovieClip._x = 30; //sets my MC's x coordinate to 30.
myMovieClip._y = 120; //sets my MC's y coordinate to 120.

}

-Ex: if (myVariable < 10){

myMovieClip._x = 30; //sets my MC's x coordinate to 30.
myMovieClip._y = 120; //sets my MC's y coordinate to 120.

}else{

myMovieClip._x = originalX; //sets my MC's x coordinate to origin.
myMovieClip._y = originalY; //sets my MC's y coordinate to origin.

}

Switch statements are also known as case statements and are used to simplify long if statements that contain many else-if's.

-Ex. of long if statement:

if (myVariable == 1){

myMovieClip._alpha = 10; //sets my MC's alpha (opacity) to 10%.

}else if (myVariable == 2){

myMovieClip._alpha = 20; //sets my MC's alpha (opacity) to 20%.

}else if (myVariable == 3){

myMovieClip._alpha = 30; //sets my MC's alpha (opacity) to 30%...etc..

}else{

myMovieClip._alpha = 100; //sets my MC's alpha (opacity) to 100%...etc..

}

-Ex. switch statement used to simplify above sample:

//in this example myVariable is an integer, so the different cases are integers.
switch (myVariable){

case 1: myMovieClip._alpha = 10; break;

case 2: myMovieClip._alpha = 20; break;

case 3: myMovieClip._alpha = 30; break;

default: myMovieClip._alpha = 100; break;

}

-Ex. switch statement using string variable:

myVariable = "one";

//in this example myVariable is a string, so the different cases are strings.
switch (myVariable){

case "one": myMovieClip._alpha = 10; break;

case "two": myMovieClip._alpha = 20; break;

case "three": myMovieClip._alpha = 30; break;

default: myMovieClip._alpha = 100; break;

}

 

Loops: while and for

• Loops are mechanisms within code that allow for an instruction or group of instructions to be repeated in an efficient manner, without excessive code.

• Timeline Loops vs. Actionscript Loops:

-Timeline loops are pieces of code that are directed at the playback head and repeat a certain section of an animation sequence.

-Actionscript loops are pieces of detailed code used to repeat events separate of the timeline, such as resetting or loading variables with a value.

• While loops are very similar to if statements: while something is true, do this until something isn't true.

-Ex:

i = 0; //declaring and initializing variable i.

while (i < 10){ //this statement will execute the following instructions 10 times.

myMovieClip._x = myMovieClip._x + 10; //changes x coordinate by 10 each time.

i++; /*increments i by 1 each time until i is no longer less than 10. You must increment i or you will end up with a run-time error, meaning your loop will never end and the computer may crash.*/

}

• For loops are similar to while loops, but contain all the necessary information for making the loop run within the () at the beginning of the statement.

-Ex:

var i; //declaring variable i.

for (i = 0; i < 10; i++){ //(initialize var; conditional; increment var)

//updates x coordinate by 10 each time.
myMovieClip._x = myMovieClip._x + 10;

}

-Ex. of a reverse loop:

var counter; //declaring variable counter.

//will count down from 10 to 1 before exiting the loop.
for (counter = 10; counter > 0; counter--){

//places the value of counter in the output window when the movie is tested.
trace(counter);

}

-Ex. using a for loop to load an array variable:

myArray = new Array(); //declaring an array.

for (i = 0; i < 10; i++){ //(initialize var; conditional; increment var)

//i acts as an index as well as a variable that is part of an expression.
myArray[i] = i * .10;

trace(myArray); //can be used to check that the array is loading properly.

}