Statements & loops Flash ActionScript Introduction to.

10
Statements & loops Statements & loops Flash ActionScript Flash ActionScript Introduction to Introduction to

Transcript of Statements & loops Flash ActionScript Introduction to.

Page 1: Statements & loops Flash ActionScript Introduction to.

Statements & loopsStatements & loops

Flash ActionScriptFlash ActionScriptIntroduction toIntroduction to

Page 2: Statements & loops Flash ActionScript Introduction to.

Conditional StatementsConditional Statements

A way for the computer to make a choice based on A way for the computer to make a choice based on some condition ”If it is dark, then light the lamp, if some condition ”If it is dark, then light the lamp, if not, leave it off.”not, leave it off.”

The The if - statementif - statement is the most important is the most important conditional statement in ActionScriptconditional statement in ActionScript

if (statement_one operator statement_two){if (statement_one operator statement_two){

//run code//run code

}}

Page 3: Statements & loops Flash ActionScript Introduction to.

If & else statementIf & else statement Example of a if-statement with the operator “>”Example of a if-statement with the operator “>”

(If var1 is greater than var2) (If var1 is greater than var2)

if(x > 100){if(x > 100){

trace(”X is too big.”);trace(”X is too big.”);

}}

Instead of writing another if-statement you can use Instead of writing another if-statement you can use the the else- statementelse- statement to catch when the condition to catch when the condition evaluates to falseevaluates to false

if(x > 100){if(x > 100){

trace(”X is too big.”);trace(”X is too big.”);

}else{}else{

trace(”X is not too big.”);trace(”X is not too big.”);

}}

Page 4: Statements & loops Flash ActionScript Introduction to.

OperatorsOperators Relational and Equality operators, evaluates to true or

false

Operator Function == If var1 is equal to var2

>= If var1 is greater than or equal to var2 > If var1 is greater than var2 <= If var1 is smaller than or equal to var2 < If var1 is smaller than var2!= If var1 is NOT equal to var2&& If var1 and var2 exist or both var1 and var2 are both set to true|| If either var1 and var2 exist and/or are set to true.

Comparison operator: compares two expressions for equality

expression1 eq expression2 If(input1 eq input2){

Do someting…

Page 5: Statements & loops Flash ActionScript Introduction to.

Examples of usage…Examples of usage…if(hungry && !thirsty){if(hungry && !thirsty){

trace("go get a combo from the closest burger joint");trace("go get a combo from the closest burger joint");}}

if((variable_1 == variable_2) && (variable_3 > variable_4)){ if((variable_1 == variable_2) && (variable_3 > variable_4)){ trace("variable 1 is equal to variable 2 and variable 3 is greater trace("variable 1 is equal to variable 2 and variable 3 is greater than variable 4"); than variable 4");

} }

if(theTimeOfDay == theMorning){if(theTimeOfDay == theMorning){ trace("wake up");trace("wake up");} else if(theTimeOfDay == myBedTime){} else if(theTimeOfDay == myBedTime){ trace("go to bed");trace("go to bed");} else if(iShouldBeAtWork){} else if(iShouldBeAtWork){ trace("working right now");trace("working right now");} else {} else { Trace("watch some tv");Trace("watch some tv");}}

Page 6: Statements & loops Flash ActionScript Introduction to.

LoopsLoops ActionScript loops are used to execute a segment ActionScript loops are used to execute a segment

of code repeatedly for a number of times or while a of code repeatedly for a number of times or while a certain condition is satisfiedcertain condition is satisfied

This can save time and effort by not having to type This can save time and effort by not having to type the same code multiple times to repeat a processthe same code multiple times to repeat a process

Example of loops: Example of loops: For - loopFor - loop

While - loop While - loop

Do/While loopDo/While loop

Page 7: Statements & loops Flash ActionScript Introduction to.

For - loopFor - loop

The for-loop is probably the most commonly used The for-loop is probably the most commonly used because it is the most compact and the easiest to because it is the most compact and the easiest to understand and useunderstand and use

StructureStructure

for (counter; condition; action){for (counter; condition; action){statements;statements;

} }

ExampleExample

for (i=0; i < 10; i++){for (i=0; i < 10; i++){trace ("This code is repeated ten times");trace ("This code is repeated ten times");

}}

Page 8: Statements & loops Flash ActionScript Introduction to.

While - loopWhile - loop

The While - loop repeats a set of code as long as The While - loop repeats a set of code as long as the condition specified is truethe condition specified is true

StructureStructure

while (condition) {while (condition) {statementsstatements

} }

ExampleExample

var i = 1;var i = 1;while (i < 5){while (i < 5){

trace ("This code is repeated");trace ("This code is repeated");i++;i++;

} }

Page 9: Statements & loops Flash ActionScript Introduction to.

Do - While loopDo - While loop

A Do While loop will take a comparison statement A Do While loop will take a comparison statement and as long as the statement returns true, the loop and as long as the statement returns true, the loop will runwill run

StructureStructure

do { do { statements;statements;} while (condition); } while (condition);

ExampleExample

var i = 1;var i = 1;do { do {

trace ("This code is repeated");trace ("This code is repeated");i++;i++;

}while(i<5);}while(i<5);

Page 10: Statements & loops Flash ActionScript Introduction to.

Examples of loop UsageExamples of loop Usage

Loops are very often used to manipulate and Loops are very often used to manipulate and access the content of arraysaccess the content of arrays

var movie_array = new Array();var movie_array = new Array();movie_array = [“Batman", “The Godfather", “Star Wars", “Lord of The movie_array = [“Batman", “The Godfather", “Star Wars", “Lord of The

Rings", "Titanic"];Rings", "Titanic"];

for (i=0; i < movie_array.length; i++){for (i=0; i < movie_array.length; i++){trace (movie_array[i]);trace (movie_array[i]);

}}

Another common use of loops involves the creation Another common use of loops involves the creation and control of dynamic movieclipsand control of dynamic movieclips

for (i = 1; i < 6; i++){for (i = 1; i < 6; i++){duplicateMovieClip("movie_mc", "new_“ + i, i);duplicateMovieClip("movie_mc", "new_“ + i, i);this["new_“ + i]._x = this["new_“ + i]._width * i;this["new_“ + i]._x = this["new_“ + i]._width * i;

}}