170 likes | 290 Vues
Dive into the fundamentals of Flash ActionScript 3.0 with a focus on essential conditional statements and loops. Understand how to make choices in your code using "if", "else", and "switch" statements, allowing your programs to respond intelligently to various conditions. Learn about operators and how to construct complex logical expressions. Additionally, explore loops like "for", "while", and "do-while" to efficiently execute code repeatedly, manage arrays, and manipulate strings. Perfect for aspiring Flash developers!
E N D
Introduction to Flash ActionScript 3.0 Statements & Loops Thomas Lövgren, Flash developer thomas.lovgren@humlab.umu.se
Conditional Statements • A way for the computer to make a choice based on some condition ”If it is dark, then light the lamp, if not, leave it off.” • The operator checks a specific condition for the statement(s) • The condition could be either true or false depending on the setup • The if - statement is the most important conditional statement in ActionScript if (statement_one operator statement_two){ //run code }
If & else statement • Example of a if-statement with the operator ">" (If var1 is greater than var2) if(x > 100){ //statement trace("X is too big"); //output } • Instead of writing another if-statement you can use the else- statement to catch when the condition evaluates to false if(x > 100){ trace("X is too big"); }else{ trace("X is not too big"); }
Operators • Relational and Equality operators, evaluates to true or false Operator Function/task == 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
Statements examples (1/2) if(hungry && !thirsty){ trace("go get a combo from the closest burger joint"); } if((variable_1 == variable_2) && (variable_3 > variable_4)){ trace("variable 1 is equal to variable 2 and variable 3 is greater than variable 4"); } if(theTimeOfDay == theMorning){ trace("wake up"); } else if(theTimeOfDay == myBedTime){ trace("go to bed"); } else if(iShouldBeAtWork){ trace("working right now"); } else { trace("watch some tv"); }
Statements examples (2/2) //declare variables and assign values varvideoLoaded:Boolean = false; vargetData:Boolean = false; //if statement if(videoLoaded){ //is videoLoaded true? trace("The video is loaded"); //will not execute } if(!getData){ //is getData false? trace("The getData variable is false"); //will execute }
Switch Statement • Switch Staements are useful when you want to respond to a series of possible values that a variable might have (instead of writing long If-structures) varswitchExpression:int = 3; //declare variable switch(switchExpression){ //switch statement case 0: trace(0); break; case 1: trace(1); break; case 2: trace(2); break; default: trace("not case 0, 1, or 2"); //traces not case 0, 1, or 2 }
Loops • ActionScript loops are used to execute a segment of code repeatedly for a number of times or while a certain condition is satisfied • This can save time and effort by not having to type the same code multiple times to repeat a process • Example of loops: • For - loop • While - loop • Do/While loop
Loops • Flowchart example of a loop
For - loop • The for-loop is probably the most commonly used because it is the most compact and the easiest to understand and use • Structure for (counter; condition; action){ statements; } • Example for (var i:int = 0; i < 10; i++){ //for-loop trace ("This code is repeated ten times"); //output }
While - loop • The While - loop repeats a set of code as long as the condition specified is true • Structure while (condition) { statements } • Example var i:int = 1; while (i < 5){ //condition trace ("This code is repeated"); //output i++; //increase counter }
Do - While loop • A Do While loop will take a comparison statement and as long as the statement returns true, the loop will run • Structure do { statements; } while (condition); • Example var i:int = 1; do { trace ("This code is repeated"); //output i++; //increase counter }while(i < 5); //condition
Reverse Loop & statement • It’s also posible to count down by reversing the values, and then decrementing the counter //reverse loop for (var i:int = 10; i > 0; i--){ trace("Hello: " + i); //output } • Example of an If-statement inside a for-loop: for (var i:int = 0; i < 100; i++){ //for-loop if(i > 50){ //if-statement trace(i + ": is greater than 50"); //output } }
Array & Loops • Loops are very often used to manipulate and access the content of arrays varmovie_array = new Array(); movie_array = ["Batman", "The Godfather", "Star Wars", "Lord of The Rings", "Titanic"]; //loop through the array for (var i:int = 0; i < movie_array.length; i++){ trace (movie_array[i]); //output } /* Batman The Godfather Star Wars Lord of The Rings */ Titanic
String & Loops • We can use a loop to go through a text string, and for example replace a word or a specific character //declare string and assign value varbook_string:String = "IT by Stephen King"; for (var i:int = 0; i < book_string.length; i++){ trace("Character " + i + ": " + book_string.charAt(i)); } /* output Character 0: I Character 1: T Character 2: Character 3: b …… */
Named Loops • By naming a loop when you call break you can target the named loop as the break. This example shows a Nested for-loop: //my top loop outsideLoop:for(var i:int = 0; i < 10; i++){ trace(i + ": called from outsideLoop"); //output //my inside loop insideLoop:for(var j:int = 0; j < 5; j++){ trace(j + ": called from insideLoop"); if(j < 2) break outsideLoop; //stop the outside loop } }
New Loops in AS3 • Examples of the new loop construct in AS3: //set up the array varmy_array:Array = ["test", "test2", "test3"]; //use a regular for in loop to access the properties in my_array for (varelement:String in my_array){ trace(my_array[element]); //traces test, test2, test3 } //use the new for each in loop to access the values in my_array for each (varindexData:String in my_array){ trace(indexData); //traces test, test2, test3 }